Android界面编程之利用单选框和复选框实现对学历和爱好进行选择

Android界面编程之利用单选框和复选框实现对学历和爱好进行选择

首先我们要了解一下单选框和复选框:

单选框(Radio Button):当用户选择某单选按钮时,同一组中的其他单选按钮不能同时选定,通常进行单选的按钮放到一个RadioGroup中

复选框(Check Box):可以选择任意数目,没有必要放到一个组中

定位被选中的按钮
单选框哪个按钮被选中时,需要在组中进行选择,复选框则不需要,所以单选框要对组进行监听,而复选框可以对个体进行监听,也可以创建一个数组用来循环扫描是否选中。

我使用数组来循环扫面,就不可以创建全局的变量了,不过写起来简单些,复制粘贴,出于代码的规范性最好选择不要创建数组,可以创建几个全局变量,然后自定义一个扫描的方法,本文就不一一掩饰了

下面看代码:


布局:采用Tablelayout布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学历"
            android:textSize="20sp"/>
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:id="@+id/small"
                android:text="小学"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/midle"
                android:text="中学"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/large"
                android:text="大学"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/secises"
                android:text="研究生"/>
        </RadioGroup>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <CheckBox
                android:id="@+id/run"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="跑步" />

            <CheckBox
                android:id="@+id/swim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="游泳" />

            <CheckBox
                android:id="@+id/play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打球" />

            <CheckBox
                android:id="@+id/read"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="读书" />

        </LinearLayout>
    </TableRow>
    <TextView
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</TableLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    String stu;
    String hob;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablelayout);
        final TextView info =(TextView)findViewById(R.id.info);
        RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup1);
        final CheckBox checkBox[] = new CheckBox[4];
        checkBox[0] = (CheckBox)findViewById(R.id.swim);
        checkBox[1] = (CheckBox)findViewById(R.id.run);
        checkBox[2] = (CheckBox)findViewById(R.id.read);
        checkBox[3] = (CheckBox)findViewById(R.id.play);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                if (i == R.id.small)
                    stu="您的学历是小学";
                else if (i ==R.id.midle)
                    stu="您的学历是中学";
                else if (i ==R.id.large)
                    stu="您的学历是大学";
                else if (i==R.id.secises)
                    stu="您的学历是研究生";
                info.setText(print());
            }
        });
        checkBox[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hob = null;
                if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())
                    hob="您的爱好是: ";
                for (int k = 0 ; k < checkBox.length ; k++ ){
                    if (checkBox[k].isChecked())
                        hob=hob+checkBox[k].getText().toString()+" ";
                }
                info.setText(print());
            }
        });
        checkBox[1].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hob = null;
                if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())
                    hob="您的爱好是: ";
                for (int k = 0 ; k < checkBox.length ; k++ ){
                    if (checkBox[k].isChecked())
                        hob=hob+checkBox[k].getText().toString()+" ";
                }
                info.setText(print());
            }
        });
        checkBox[2].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hob = null;
                if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())
                    hob="您的爱好是: ";
                for (int k = 0 ; k < checkBox.length ; k++ ){
                    if (checkBox[k].isChecked())
                        hob=hob+checkBox[k].getText().toString()+" ";
                }
                info.setText(print());
            }
        });
        checkBox[3].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hob = null;
                if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())
                    hob="您的爱好是: ";
                for (int k = 0 ; k < checkBox.length ; k++ ){
                    if (checkBox[k].isChecked())
                        hob=hob+checkBox[k].getText().toString()+" ";
                }
                info.setText(print());
            }
        });
    }
    public String print(){
        String str = null;
        if(stu == null && hob == null){
            str = "请选择您的学历和爱好" ;
        }
        else if (stu != null && hob == null){
            str = stu ;
        }
        else if (stu == null && hob != null){
            str = hob ;
        }
        else {
            str = stu + "\n" + hob;
        }
        return str;
    }

}

实现效果: 每次更改单选框或复选框都会更新提示
这里写图片描述

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值