android 单选按钮、复选按钮的使用
作者:互联网
一、单选按钮
相互排斥的事件使用该控件,一组相互互斥的事件放到一个组内,及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