经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android控件与布局——基础控件RadioButton
来源:cnblogs  作者:yooooooo  时间:2020/12/8 8:44:55  对本文有异议

今天,我们的主题是基础控件RadioButton。在开始之前,我们还是以官方文档为开端来开始我们的讲解,下面是Android文档中对RadioButton的简介:

看过上一篇文章的应该可以了解到,这个和我们的CheckBox是十分类似的,不同的点在于,这个控件可以由非选中状态通过点击事件转为选中状态,但是不能通过点击实现逆向的状态转换,一个默认样式RadioButton控件的非选中和选中状态如下:

image

其组成和CheckBox一样,我们同样可以分别对其中的字体和Button进行设置,实现达到和CheckBox一样的效果。在上面我们在简介中得知,这个控件能通过点击事件实现的效果如下(不能逆向改变状态):

image

接下来,我们对其基本属性进行设置,改变一下它的样式:

image

下面我们就结合一个小例子来实际的应用一下,这个小例子就是实现多项单选功能,运行的效果如下:

image

布局文件与控制逻辑如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButtonActivity">
  9. <TextView
  10. android:layout_marginTop="20dp"
  11. android:textSize="20sp"
  12. android:text="请选择您最爱的职业:"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. <RadioButton
  16. android:textSize="25sp"
  17. android:id="@+id/radio_button1"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textColor="@color/colorPrimaryDark"
  21. android:layout_marginStart="16dp"
  22. android:layout_marginTop="20dp"
  23. android:text="程序员" />
  24. <RadioButton
  25. android:textSize="25sp"
  26. android:id="@+id/radio_button2"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:textColor="@color/colorPrimaryDark"
  30. android:layout_marginStart="16dp"
  31. android:layout_marginTop="11dp"
  32. android:text="政治家" />
  33. <RadioButton
  34. android:textSize="25sp"
  35. android:id="@+id/radio_button3"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:textColor="@color/colorPrimaryDark"
  39. android:layout_marginStart="16dp"
  40. android:layout_marginTop="11dp"
  41. android:text="富二代" />
  42. </LinearLayout>
  1. package aoto.com.commonwidgetandlayout.basic_widget.radioButton;
  2. import android.graphics.Color;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.CompoundButton;
  7. import android.widget.RadioButton;
  8. import aoto.com.commonwidgetandlayout.R;
  9. /**
  10. * @author why
  11. * @date 2019-5-13 20:44:28
  12. */
  13. public class RadioButtonActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
  14. private static final String TAG = "RadioButtonActivityWhy";
  15. RadioButton radioButton1;
  16. RadioButton radioButton2;
  17. RadioButton radioButton3;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_radio_button);
  22. radioButton1=findViewById(R.id.radio_button1);
  23. radioButton2=findViewById(R.id.radio_button2);
  24. radioButton3=findViewById(R.id.radio_button3);
  25. radioButton1.setOnCheckedChangeListener(this);
  26. radioButton2.setOnCheckedChangeListener(this);
  27. radioButton3.setOnCheckedChangeListener(this);
  28. }
  29. @Override
  30. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  31. if(isChecked){
  32. disableOthers(buttonView.getId());
  33. Log.e(TAG, "您最喜欢的职业是: "+buttonView.getText().toString() );
  34. buttonView.setTextColor(getResources().getColor(R.color.colorPrimary));
  35. }
  36. else {
  37. buttonView.setTextColor(Color.BLACK);
  38. }
  39. }
  40. private void disableOthers(int viewId) {
  41. if(R.id.radio_button1!=viewId&&radioButton1.isChecked()){
  42. radioButton1.setChecked(false);
  43. }
  44. if(R.id.radio_button2!=viewId&&radioButton2.isChecked()){
  45. radioButton2.setChecked(false);
  46. }
  47. if(R.id.radio_button3!=viewId&&radioButton3.isChecked()){
  48. radioButton3.setChecked(false);
  49. }
  50. }
  51. }

可见,我们为了实现一个单选功能做了很多逻辑控制,而这样的场景又非常多,没有关系,我们接着官方文档关于对其的介绍继续向下看:

  1. * <p>
  2. * Radio buttons are normally used together in a
  3. * {@link android.widget.RadioGroup}. When several radio buttons live inside
  4. * a radio group, checking one radio button unchecks all the others.</p>

说这个RadioButton经常会结合RadioGroup一起使用,实现的功能正是我们上面所要实现的多项单选功能的操作。那下面就来看看如何使用RadioGroup实现上述例子的功能:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButton2Activity">
  9. <TextView
  10. android:layout_marginLeft="40dp"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="20dp"
  14. android:text="请选择您最爱的职业:"
  15. android:textSize="20sp" />
  16. <RadioGroup
  17. android:id="@+id/job_list"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center_horizontal">
  21. <RadioButton
  22. android:id="@+id/radio_button1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginStart="16dp"
  26. android:layout_marginTop="20dp"
  27. android:text="程序员"
  28. android:textColor="@color/colorPrimaryDark"
  29. android:textSize="25sp" />
  30. <RadioButton
  31. android:id="@+id/radio_button2"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_marginStart="16dp"
  35. android:layout_marginTop="11dp"
  36. android:text="政治家"
  37. android:textColor="@color/colorPrimaryDark"
  38. android:textSize="25sp" />
  39. <RadioButton
  40. android:id="@+id/radio_button3"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_marginStart="16dp"
  44. android:layout_marginTop="11dp"
  45. android:text="富二代"
  46. android:textColor="@color/colorPrimaryDark"
  47. android:textSize="25sp" />
  48. </RadioGroup>
  49. <Button
  50. android:id="@+id/clear_all"
  51. android:text="都不喜欢"
  52. android:onClick="clearAll"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content" />
  55. </LinearLayout>

说这个RadioButton经常会结合RadioGroup一起使用,实现的功能正是我们上面所要实现的多项单选功能的操作。那下面就来看看如何使用RadioGroup实现上述例子的功能:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButton2Activity">
  9. <TextView
  10. android:layout_marginLeft="40dp"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="20dp"
  14. android:text="请选择您最爱的职业:"
  15. android:textSize="20sp" />
  16. <RadioGroup
  17. android:id="@+id/job_list"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center_horizontal">
  21. <RadioButton
  22. android:id="@+id/radio_button1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginStart="16dp"
  26. android:layout_marginTop="20dp"
  27. android:text="程序员"
  28. android:textColor="@color/colorPrimaryDark"
  29. android:textSize="25sp" />
  30. <RadioButton
  31. android:id="@+id/radio_button2"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_marginStart="16dp"
  35. android:layout_marginTop="11dp"
  36. android:text="政治家"
  37. android:textColor="@color/colorPrimaryDark"
  38. android:textSize="25sp" />
  39. <RadioButton
  40. android:id="@+id/radio_button3"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_marginStart="16dp"
  44. android:layout_marginTop="11dp"
  45. android:text="富二代"
  46. android:textColor="@color/colorPrimaryDark"
  47. android:textSize="25sp" />
  48. </RadioGroup>
  49. <Button
  50. android:id="@+id/clear_all"
  51. android:text="都不喜欢"
  52. android:onClick="clearAll"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content" />
  55. </LinearLayout>

逻辑部分:

  1. package aoto.com.commonwidgetandlayout.basic_widget.radioButton;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.RadioButton;
  7. import android.widget.RadioGroup;
  8. import aoto.com.commonwidgetandlayout.R;
  9. /**
  10. * @author why
  11. * @date 2019-5-13 21:43:42
  12. */
  13. public class RadioButton2Activity extends AppCompatActivity {
  14. private static final String TAG = "RadioButton2ActivityWhy";
  15. RadioGroup radioGroup;
  16. RadioButton radioButton1;
  17. RadioButton radioButton2;
  18. RadioButton radioButton3;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_radio_button2);
  23. radioGroup=findViewById(R.id.job_list);
  24. radioButton1=findViewById(R.id.radio_button1);
  25. radioButton2=findViewById(R.id.radio_button2);
  26. radioButton3=findViewById(R.id.radio_button3);
  27. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  28. @Override
  29. public void onCheckedChanged(RadioGroup group, int checkedId) {
  30. getYourFavorite(checkedId);
  31. }
  32. });
  33. }
  34. /**
  35. * 根据ID,执行相应的逻辑
  36. * @param buttonId
  37. */
  38. private void getYourFavorite(int buttonId){
  39. switch (buttonId){
  40. case R.id.radio_button1:
  41. if(radioButton1.isChecked()) {
  42. Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString());
  43. }
  44. break;
  45. case R.id.radio_button2:
  46. if(radioButton2.isChecked()) {
  47. Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString());
  48. }
  49. break;
  50. case R.id.radio_button3:
  51. if(radioButton3.isChecked()) {
  52. Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString());
  53. }
  54. break;
  55. }
  56. }
  57. /**
  58. * 清除选型
  59. * @param view
  60. */
  61. public void clearAll(View view){
  62. radioGroup.clearCheck();
  63. }
  64. }

在布局部分,我们只需要把之前放置在布局中的RadioButton放置在RadioGroup中即可:

  1. <RadioGroup
  2. android:id="@+id/job_list"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:gravity="center_horizontal">
  6. <RadioButton
  7. android:id="@+id/radio_button1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginStart="16dp"
  11. android:layout_marginTop="20dp"
  12. android:text="程序员"
  13. android:textColor="@color/colorPrimaryDark"
  14. android:textSize="25sp" />
  15. <RadioButton
  16. android:id="@+id/radio_button2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginStart="16dp"
  20. android:layout_marginTop="11dp"
  21. android:text="政治家"
  22. android:textColor="@color/colorPrimaryDark"
  23. android:textSize="25sp" />
  24. <RadioButton
  25. android:id="@+id/radio_button3"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_marginStart="16dp"
  29. android:layout_marginTop="11dp"
  30. android:text="富二代"
  31. android:textColor="@color/colorPrimaryDark"
  32. android:textSize="25sp" />
  33. </RadioGroup>

逻辑部分我们首先为RadioGroup设置状态变化监听:

  1. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  2. @Override
  3. public void onCheckedChanged(RadioGroup group, int checkedId) {
  4. getYourFavorite(checkedId);
  5. }
  6. });

根据选择的RadioButton的ID执行具体的逻辑代码:

  1. /**
  2. * 根据ID,执行相应的逻辑
  3. * @param buttonId
  4. */
  5. private void getYourFavorite(int buttonId){
  6. switch (buttonId){
  7. case R.id.radio_button1:
  8. if(radioButton1.isChecked()) {
  9. Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString());
  10. }
  11. break;
  12. case R.id.radio_button2:
  13. if(radioButton2.isChecked()) {
  14. Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString());
  15. }
  16. break;
  17. case R.id.radio_button3:
  18. if(radioButton3.isChecked()) {
  19. Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString());
  20. }
  21. break;
  22. }
  23. }

注意到在这里我们只实现了数据的获取(RadioButton的文本内容),RadioGroup中的RadioButton之间的状态管理(单选)是RadioGroup内部自己管理的,这为我们节省很多的开发逻辑,也是我们用它的主要目的。此外,这里,我们还可以通过调用clearCheck()实现清除选择状态。

  1. /**
  2. * 根据ID,执行相应的逻辑
  3. * @param buttonId
  4. */
  5. private void getYourFavorite(int buttonId){
  6. switch (buttonId){
  7. case R.id.radio_button1:
  8. if(radioButton1.isChecked()) {
  9. Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString());
  10. }
  11. break;
  12. case R.id.radio_button2:
  13. if(radioButton2.isChecked()) {
  14. Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString());
  15. }
  16. break;
  17. case R.id.radio_button3:
  18. if(radioButton3.isChecked()) {
  19. Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString());
  20. }
  21. break;
  22. }
  23. }

注意到在这里我们只实现了数据的获取(RadioButton的文本内容),RadioGroup中的RadioButton之间的状态管理(单选)是RadioGroup内部自己管理的,这为我们节省很多的开发逻辑,也是我们用它的主要目的。此外,这里,我们还可以通过调用clearCheck()实现清除选择状态。

  1. radioGroup.clearCheck()

运行结果如下所示:

image

同样,如果你觉得RadioButton中的Button样式不好看,你可以自定义一种,这里,我们还是选用上一篇中的样式代码,执行效果如下:

image

修改按钮样式是通过android:button属性:

  1. <RadioButton
  2. android:id="@+id/radio_button1"
  3. android:button="@drawable/check_box_back"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_marginStart="16dp"
  7. android:layout_marginTop="20dp"
  8. android:text="程序员"
  9. android:textColor="@color/colorPrimaryDark"
  10. android:textSize="25sp" />

其中的check_box_back.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="true" android:drawable="@drawable/chosen"></item>
  4. <item android:state_checked="false" android:drawable="@drawable/non_chosen_big"></item>
  5. </selector>

该控件的开源项目在网上找了一下,感觉没有什么比较好的,主要是因为它的封装程度已经很高了,如果只是想改动一下显示样式和逻辑,我们自己完全可以实现。好了,关于RadioButton到这里的简单介绍就介绍了。

原文链接:http://www.cnblogs.com/linhaostudy/p/14068975.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号