实现单选框的功能需要RadioButton和RadioGroup配合使用。RadioGroup是单选组合框,是可以容纳多个RadioButton的容器。在没有RadioGroup的情况下, RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听。
RadioGroup的函数:
- RadioGroup.getCheckedRadioButtonId (); //获取选中按钮的id
- RadioGroup.clearCheck (); //清除选中状态
- RadioGroup.check (int id); //通过参入选项id来设置该选项为选中状态,如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作;checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件
- RadioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener); //一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
- addView (View child, int index, ViewGroup.LayoutParams params); //使用指定的布局参数添加一个子视图;参数 child 为所要添加的子视图; index 将要添加子视图的位置;params 所要添加的子视图的布局参数
- RadioButton.getText(); //获取单选框的值
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我选择的是:"
android:textSize="32sp" />
<RadioGroup
android:id="@+id/rg_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/rb_boy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Boy" />
<RadioButton
android:id="@+id/rb_girl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Girl" />
</RadioGroup>
<Button
android:id="@+id/btn1"
android:background="@drawable/button_selector"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="清除选中按钮"
android:textColor="@color/test_color_selector"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/btn2"
android:background="@drawable/button_selector"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="添加单选项"
android:textColor="@color/test_color_selector"
android:layout_marginTop="20dp" />
</LinearLayout>
2.实现代码
package com.example.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.test1.util.Arith;
public class MainActivity extends AppCompatActivity
implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
private TextView tv_result;
private RadioGroup rg_choose;
private RadioButton radioButton_boy,radioButton_girl;
private Button clearBtn;
private Button addBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
tv_result = (TextView) findViewById(R.id.tv_result);
rg_choose = (RadioGroup) findViewById(R.id.rg_choose);
radioButton_boy=(RadioButton)findViewById(R.id.rb_boy);
radioButton_girl=(RadioButton)findViewById(R.id.rb_girl);
clearBtn = (Button) findViewById(R.id.btn1);
addBtn = (Button) findViewById(R.id.btn2);
//设置监听器
rg_choose.setOnCheckedChangeListener(this);
clearBtn.setOnClickListener(this);
addBtn.setOnClickListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取对应id
int id=group.getCheckedRadioButtonId();
switch (id){
case R.id.rb_boy:
tv_result.setText("你的选择是:" + radioButton_boy.getText());
break;
case R.id.rb_girl:
tv_result.setText("你的选择是:" + radioButton_girl.getText());
break;
default:
tv_result.setText("你的选择是:新增");
break;
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn1){
//清除勾选状态
rg_choose.check(-1);
rg_choose.clearCheck();
tv_result.setText("你的选择是:");
}
if(v.getId() == R.id.btn2){
//添加单选按钮
RadioButton radio = new RadioButton(this);
radio.setText("新增");
rg_choose.addView(radio,rg_choose.getChildCount());
}
}
}