ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android 单选按钮、复选按钮的使用

2021-10-31 15:04:47  阅读:177  来源: 互联网

标签:findViewById tv cb setText break 单选 按钮 android id


一、单选按钮
相互排斥的事件使用该控件,一组相互互斥的事件放到一个组内,及RadioGroup。


    <RadioGroup
        android:id="@+id/radiogroup_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/rb_option1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="about" />

        <RadioButton
            android:id="@+id/rb_option2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="on" />

        <RadioButton
            android:id="@+id/rb_option3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="with" />

    </RadioGroup>


添加点击事件:

  public class RadioButtonTextActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private TextView tv_question,tv_answer;
private RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button_text);
        initView();

    }
    //初始化对象
    private void initView(){
        tv_answer=findViewById(R.id.tv_answer);
        radioGroup=findViewById(R.id.radiogroup_text);
        //添加监听器
        radioGroup.setOnCheckedChangeListener(this);
    }
  @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch(i){
            case R.id.rb_option1:
                tv_answer.setText("A");
                break;
            case R.id.rb_option2:
                tv_answer.setText("B");
                break;
            case R.id.rb_option3:
                tv_answer.setText("C");
                break;
        }
    }
    }

在这里插入图片描述
二、复选框

<CheckBox
    android:id="@+id/checkbox_A"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ipad"
    />
    <CheckBox
        android:id="@+id/checkbox_B"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="华为荣耀手机"
        />
    <CheckBox
        android:id="@+id/checkbox_C"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Nike品牌运动鞋"
        />

activity中添加点击事件

public class CheckBoxTestActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private TextView tv_choice1,tv_choice2,tv_choice3;
private CheckBox cb_one,cb_two,cb_three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box_test);
        initView();
    }

    public void initView() {
        tv_choice1=findViewById(R.id.tv_choice1);
        tv_choice2=findViewById(R.id.tv_choice2);
        tv_choice3=findViewById(R.id.tv_choice3);
        cb_one=findViewById(R.id.checkbox_A);
        cb_two=findViewById(R.id.checkbox_B);
        cb_three=findViewById(R.id.checkbox_C);

        cb_one.setOnCheckedChangeListener(this);
        cb_two.setOnCheckedChangeListener(this);
        cb_three.setOnCheckedChangeListener(this);
    }

    //添加复选框点击事件
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch(compoundButton.getId()){
            case R.id.checkbox_A:
                if(b){
                    tv_choice1.setText("A");
                }
                else{
                    tv_choice1.setText("");
                }
                break;
            case R.id.checkbox_B:
                if(b){
                    tv_choice2.setText("B");
                }
                else{
                    tv_choice2.setText("");
                }
                break;
            case R.id.checkbox_C:
                if(b){
                    tv_choice3.setText("C");
                }
                else{
                    tv_choice3.setText("");
                }
                break;
            default:
                break;
        }
    }
}

在这里插入图片描述
以上就是简单的介绍下单选以及复选按钮的使用方法,读者可以借鉴学习使用

标签:findViewById,tv,cb,setText,break,单选,按钮,android,id
来源: https://blog.csdn.net/wuwndj/article/details/121064024

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有