Android RadioGroup:单选按钮控件

矫情吗;* 2021-12-10 12:07 521阅读 0赞

RadioGroup 为单项选择按钮组,其中可以包含多个 RadioButton,即单选按钮,它们共同为用户提供一种多选一的选择方式。

在多个 RadioButton 被同一个 RadioGroup 包含的情况下,多个 RadioButton 之间自动形成互斥关系,仅有一个可以被选择。

单选按钮的使用方法和 CheckBox 的使用方法高度相似,其事件监听接口使用的是 RadioGroup.OnCheckedChangeListener(),使用 setOnCheckedChangeListener() 方法将监听器设置到单选按钮上。

按照 CheckBox 的讲解思路,启动一个名为 RadioGroupActivity 的 Activity 来对 RadioGroup 进行讲解。

RadioGroupActivity 的运行效果如图 1 所示。

RadioGroup的应用界面
图 1 RadioGroup 的应用界面

在工程 WidgetDemo 的布局文件 main.xml 中添加一个 Button,并启动 RadioGroupActivity 的相关代码。

在 main.xml 中添加代码如下:

  1. <Button
  2. android:id=”@+id/button3”
  3. android:layout_width=”wrap_content”
  4. android:layout_height=”wrap_content”
  5. android:text=”RadioGroupDemo”/>

启动处理 RadioGroup 的 Activity RadioGroupActivity 的代码如下:

  1. Button radiotn = (Button)this.findViewById(R.id.button3);
  2. radiotn.setOnClickListener(new OnClickListener(){
  3. @Override
  4. public void onClick(View v){
  5. Intent intent = new Intent(WidgetDemoActivity.this,RadioGroupActivity.class);
  6. startActivity(intent);
  7. }
  8. })

同时在 AndroidManifest.xml 文件中声明该 Activity:

RadioGroupActivity 使用的是 radiogroup.xml,其代码如下:

  1. <?xml version=”1.0” encoding=”utf-8”?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android“
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:orientation=”vertical”>
  6. <TextView
  7. android:id=”@+id/radiohello”
  8. android:layout_width=”fill_parent”
  9. android:layout_height=”wrap_content”
  10. android:text=”@string/hello” />
  11. <RadioGroup
  12. android:id=”@+id/radiogroup1”
  13. android:layout_width=”wrap_content”
  14. android:layout_height=”wrap_content”
  15. android:layout_x=”3px”
  16. android:orientation=”vertical”>
  17. <RadioButton
  18. android:id=”@+id/radiobutton1”
  19. android:layout_width=”wrap_content”
  20. android:layout_height=”wrap_content”
  21. android:text=”@string/music” />
  22. <RadioButton
  23. android:id=”@+id/radiobutton2”
  24. android:layout_width=”wrap_content”
  25. android:layout_height=”wrap_content”
  26. android:text=”@string/gym” />
  27. <RadioButton
  28. android:id=”@+id/radiobutton3”
  29. android:layout_width=”wrap_content”
  30. android:layout_height=”wrap_content”
  31. android:text=”@string/dance” />
  32. <RadioButton
  33. android:id=”@+id/radiobutton4”
  34. android:layout_width=”wrap_content”
  35. android:layout_height=”wrap_content”
  36. android:text=”@string/lookBook” />

该布局文件使用了 LinearLayout 布局,并且在其中放置了一个 TextView 和一个 RadioGroup。RadioGroup 中含有三个 RadioButton。这些组件对应的 strings.xml 文件中定义的变量为:

  1. 选择你最喜欢的课程:
  2. 音乐
  3. 体育
  4. 舞蹈
  5. 看书

RadioGroupActivity.java 的代码如下:

  1. package introduction.android.widgetdemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.RadioButton;
  5. import android.widget.RadioGroup;
  6. import android.widget.TextView;
  7. public class RadioGroupActivity extends Activity {
  8. private TextView textView;
  9. private RadioGroup radiogroup;
  10. private RadioButton radio1,radio2,radio3,radio4;
  11. @Override
  12. protected void onCreate(Bundle saveInstanceState) {
  13. super.onCreate(saveInstanceState);
  14. this.setContentView(R.layout.radiogroup);
  15. textView = (TextView) findViewById(R.id.radiohello);
  16. radiogroup = (RadioGroup)findViewById(R.id.radiogroup1);
  17. radio1 = (RadioButton) findViewById(R.id.radiobutton1);
  18. radio2 = (RadioButton) findViewById(R.id.radiobutton2);
  19. radio3 = (RadioButton) findViewById(R.id.radiobutton3);
  20. radio4 = (RadioButton) findViewById(R.id.radiobutton4);
  21. radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  22. @Override
  23. public void onCheckedChanged(RadioGroup group, int checkedId) {
  24. String text=”我最喜欢运动是”;
  25. if (checkedId == radio1.getId()) {
  26. text+=radio1.getText().toString();
  27. textView.setText(text);
  28. } else if(checkedId == radio2.getId()){
  29. text+=radio2.getText().toString();
  30. textView.setText(text);
  31. }else if(checkedId == radio3.getId()) {
  32. text += radio3.getText().toString();
  33. textView.setText(text);
  34. }else if(checkedId == radio4.getId()) {
  35. text += radio4.getText().toString();
  36. textView.setText(text);
  37. }
  38. }
  39. });
  40. }
  41. }

在 RadioGroupActivity 的 onCreate() 方法中为 RadioGroup 添加监视器 RadioGroup。

OnCheckedChangeListener 在其回调方法 onCheckedChanged() 中对 4 个 RadioButton 分别进行处理。需要说明的是,如果把 RadioGroup 去掉,只使用 RadioButton 的话,则需要为每个 RadioButton 单独设置监听器,其使用方法和 CheckBox 没有任何区别。

发表评论

表情:
评论列表 (有 0 条评论,521人围观)

还没有评论,来说两句吧...

相关阅读