为了账号安全,请及时绑定邮箱和手机立即绑定

单选按钮和多选按钮

标签:
Android

【最后结果显示在一个界面中,但是分开理解】

1、单选按钮(RadioGroup:是单选按钮组和RadioButton)


1)在activity_main.xml中

<LinearLayout                     ------------------->控件是线性布局
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <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>
</LinearLayout>
2)在strings.xml
<?xml version="1.0" encoding="utf-8" ?>
     <resources>
    <string name="hello">Hello World, Activity07!</string>
    <string name="app_name">activity07</string>
    <string name="male">男人</string>
</resources>

3)[代码]MainActivity.java代码:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */    //对控件对象进行声明
    private RadioGroup genderGroup = null;
    private RadioButton femaleButton = null;
    private RadioButton maleButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        //通过控件的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){    ------------------->如果满足if条件,那么利用Toast类来显示内容为famle的小界面
                    System.out.println("famale");         ------------------->为了更方便显示执行过程,写入了System.out.println()            Toast.makeText(MainActivity.this, "famle", Toast.LENGTH_SHORT).show();      ----------->show()方法是让它实现界面显示
                }
                else if(maleButton.getId() == checkedId)
                {
                    System.out.println("male");
                }
            }
        });
    }
}

【区别Button中的监听方法,其下只是区别的部分代码,是为了方便做出对比】

[代码]java代码:

public class MainActivity extends Activity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.calculate);
        //为按钮对象设置显示值
         button.setText("结果");
        button.setOnClickListener(new CaculateListener());
    }
        //编写监听器,是的按下按钮后就给与响应
        class CaculateListener implements View.OnClickListener {
            @Override
            public void onClick(View v) {
               //要使按钮点击后做出什么反应,都在此方法中设置
            }
        }
    }


2、CheckBox:多选按钮【注意:其不存在组的概念】

1)activity_main.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <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>

2)strings.xml

<?xml version="1.0" encoding="utf-8" ?>
     <resources>
    <string name="hello">Hello World, Activity07!</string>
    <string name="app_name">activity07</string>
    <string name="swim">swim</string>
    <string name="run">run</string>
    <string name="read">read</string>
</resources

3)[代码]MainActivity.java代码:[仅仅是多选按钮的代码】

 //为多选按钮添加监听器【为多选按钮中的为一个按钮都要设置监听器】                                【下篇日记着重介绍CompoundButton类 】swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {    ----------- >CheckBox继承了CompoundButton类 
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    ---------->区别单选按钮的监听方法的参数
                // TODO Auto-generated method stub
                if(isChecked)                             ----------------->若传入的isChecked是真,则就选中该组中的按钮
                {                                          ----------------->若传入的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");
                }
            }
        });

原文链接:http://www.apkbus.com/blog-792467-60665.html


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消