Android单选和多选择按钮:
package mars.activity07;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioTest extends Activity {
/** Called when the activity is first created. */
//对控件对象进行声明
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
//通过控件的ID来得到代表控件的对象
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
swimBox = (CheckBox)findViewById(R.id.swim);
runBox = (CheckBox)findViewById(R.id.run);
readBox = (CheckBox)findViewById(R.id.read);
//为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId() == checkedId){
System.out.println("famale");
Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId)
{
System.out.println("male");
}
}
});
//为多选按钮添加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("swim is checked");
}
else
{
System.out.println("swim is unchecked");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("run is checked");
}
else
{
System.out.println("run is unchecked");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("read is checked");
}
else
{
System.out.println("read is unchecked");
}
}
});
}
}
相关配置文件:radio.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:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
/>
</LinearLayout>
分享到:
相关推荐
android 自定义 单选按钮 radioButton
在Android中用CheckBox+LinearLayout来实现一种多行单选按钮组的效果。效果图及讲解见:https://blog.csdn.net/ahuyangdong/article/details/82691961。github源码:https://github.com/ahuyangdong/SelectCustom
实现了Android的单选按钮组的自定义和多行多列的功能。
android单选按钮与多选按钮最容易理解的模板,以此模板来学习学习
根据对文本框的监听,自动跳转单选按钮。
单选按钮实现性别的选择.rar单选按钮实现性别的选择.rar单选按钮实现性别的选择.rar
一个简单的Android调用系统自带单选按钮的listView,并实现默认选项
Android程序源码--单选、多选按钮,Android程序源码--单选、多选按钮
本文将通过radiogroup和radiobutton实现组内信息的单选, 其中radiogroup就是将radiobutton进行分组,同一管理和控制 同时实现默认选中情况,获取默认值.
android单选复选按钮数据库综合实例SpinnerDialogDemo 本示例说明: 1.实现单选按钮与后台数据绑定,通过Id绑定. 2.实现显示时默认选中项控制. 3.实现修改后保存,根据id保存. 4.数据库处理使用AHibernate1.1,详见:...
详细的安卓源代码,一个简单的单选按钮的实例。
自己顺便写的一个单选框的例子,供刚接触Android的朋友做个参考
Android 模拟的单选列表对话框,这个好像不是使用Android中固有的radio来实现的,是采用Android其它内置方法模拟出来的效果,效果与Radio组件一样,而且界面同样美观简洁,本实例只是默认的风格,有兴趣可参考以下...
主要介绍了Android编程实现带有单选按钮和复选按钮的dialog功能,结合具体实例形式分析了Android实现带有单选按钮的dialog对话框及带有复选按钮的dialog对话框相关操作技巧,需要的朋友可以参考下
单选按钮和复选按钮都是普通按钮Button的子类,所以可以使用所有Button的方法和属性。也有自己特有的属性方法 单选框 单选框就是在多个选项中只选择一个。 在Android中,单选按钮用RadioButton表示,而RadioButton类...
ListView中有一个属性:android:...* singleChoice 单选 * multipleChoice 多选 * none 默认情况,没有选中效果 在ListView的布局中设置了android:choiceMode属性后,item布局需要实现checkable,才有选中效果。
炫酷的弹出选择按钮menu
android 改变字体字体和大小,单机按钮来改变字体和大小
在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
主要为大家详细介绍了Android实现弹出列表、单选、多选框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下