您的位置:首页 > 移动开发 > Android开发

android 控件之RadioGroup&RadioButton

2014-10-29 11:40 399 查看
RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器;

2、每个RadioGroup中的RadioButton同时只能有一个被选中;

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中;

4、一个RadioGroup中至少有2个RadioButton;

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置(默认选中方法android:checked="true" );

我们在代码中使用 setOnCheckedChangeListener 来对单选按钮进行监听。

代码示例:

XML文件:

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

 <RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

 <RadioButton
android:id="@+id/radioBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioBtn1"
android:textColor="#ffffff" />

<RadioButton
android:id="@+id/radioBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioBtn2"  />
</RadioGroup>
</LinearLayout> </span>


布局格式如图:

若是希望,文字在前单选框在后,只要在main.xml布局文件中的<RadioButton/>加入

android:button="@null"

android:drawableRight="@android:drawable/btn_radio"即可

效果及如下图显示:

string.xml

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">单选按钮测试</string>
<string name="hello">你的性别是</string>
<string name="radioBtn1">男</string>
<string name="radioBtn2">女</string>
</resources> </span>


MainActivity.java

<span style="font-size:12px;">package com.android.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

//声明RadioGroup
RadioGroup raGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//通过findViewById获得RadioGroup对象
raGroup=(RadioGroup)findViewById(R.id.radioGroup);

// 添加事件监听器
raGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==R.id.radioBtn1){
Toast.makeText(MainActivity.this, "你的性别是男", Toast.LENGTH_LONG).show();
}</span><pre name="code" class="java"><span style="font-size:12px;">               else {
Toast.makeText(MainActivity.this, "你的性别是女", Toast.LENGTH_LONG).show();
}
}
});
}
} </span>




延伸:RadioButton和CheckBox的区别:

1、单个RadioButton在选中后,通过点击无法变为未选中

单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个

一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示

CheckBox在大部分UI框架中默认都以矩形表示

具体代码示例:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

 <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF" />

<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/football"
android:textSize="16sp" />

<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/basketball"
android:textSize="16sp" />

<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/volleyball"
android:textSize="16sp"/>
</LinearLayout>


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">你喜欢的运动是</string>
<string name="app_name">复选按钮测试</string>
<string name="football">足球</string>
<string name="basketball">篮球</string>
<string name="volleyball">排球</string>
</resources>


MainActivity.java

package com.android.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity{
//声明复选按钮
private CheckBox cBox1;
private CheckBox cBox2;
private CheckBox cBox3;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过findViewById获得CheckBox对象
cBox1=(CheckBox)findViewById(R.id.checkbox1);
cBox2=(CheckBox)findViewById(R.id.checkbox2);
cBox3=(CheckBox)findViewById(R.id.checkbox3);

//注册事件监听器
cBox1.setOnCheckedChangeListener(listener);
cBox2.setOnCheckedChangeListener(listener);
cBox3.setOnCheckedChangeListener(listener);

}
//响应事件
private OnCheckedChangeListener listener = new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//cBox1被选中
if (buttonView.getId()==R.id.checkbox1){
if (isChecked){
Toast.makeText(MainActivity.this, "你喜欢足球", Toast.LENGTH_LONG).show();
}
}
//cBox2被选中
else if (buttonView.getId()==R.id.checkbox2){
if (isChecked){
Toast.makeText(MainActivity.this, "你喜欢篮球", Toast.LENGTH_LONG).show();
}
}
//cBox3被选中
else if (buttonView.getId()==R.id.checkbox3){
if (isChecked){
Toast.makeText(MainActivity.this, "你喜欢排球", Toast.LENGTH_LONG).show();
}
}
}
};
}


效果图如下:

到此结束~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: