当前位置:   article > 正文

自定义RadioButton_自定义组合的radiobutton

自定义组合的radiobutton

Android提供的RadioButton样式和功能十分有限,在使用多个RadioButton进行单选的时候,RadioGroup并不好用,比如说它只能横着或者竖着排,却不能用其他排列方式。在这里自己没事瞎写一个自定义的RadioButton,实现效果如下:

 

源码如下:RadioButtonActivity.java

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.widget.CompoundButton;
  4. import android.widget.RadioButton;
  5. import android.widget.CompoundButton.OnCheckedChangeListener;
  6. public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{
  7. RadioButton rb1;
  8. RadioButton rb2;
  9. RadioButton rb3;
  10. RadioButton rb4;
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. rb1 = (RadioButton)findViewById(R.id.radioButton1);
  17. rb2 = (RadioButton)findViewById(R.id.radioButton2);
  18. rb3 = (RadioButton)findViewById(R.id.radioButton3);
  19. rb4 = (RadioButton)findViewById(R.id.radioButton4);
  20. rb1.setOnCheckedChangeListener(this);
  21. rb2.setOnCheckedChangeListener(this);
  22. rb3.setOnCheckedChangeListener(this);
  23. rb4.setOnCheckedChangeListener(this);
  24. }
  25. @Override
  26. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  27. // TODO Auto-generated method stub
  28. switch(buttonView.getId())
  29. {
  30. case R.id.radioButton1:
  31. if(isChecked == true){
  32. rb1.setChecked(isChecked);
  33. rb2.setChecked(!isChecked);
  34. rb3.setChecked(!isChecked);
  35. rb4.setChecked(!isChecked);
  36. isChecked = false;
  37. }
  38. break;
  39. case R.id.radioButton2:
  40. if(isChecked == true){
  41. rb1.setChecked(!isChecked);
  42. rb2.setChecked(isChecked);
  43. rb3.setChecked(!isChecked);
  44. rb4.setChecked(!isChecked);
  45. isChecked = false;
  46. }
  47. break;
  48. case R.id.radioButton3:
  49. if(isChecked == true){
  50. rb1.setChecked(!isChecked);
  51. rb2.setChecked(!isChecked);
  52. rb3.setChecked(isChecked);
  53. rb4.setChecked(!isChecked);
  54. isChecked = false;
  55. }
  56. break;
  57. case R.id.radioButton4:
  58. if(isChecked == true){
  59. rb1.setChecked(!isChecked);
  60. rb2.setChecked(!isChecked);
  61. rb3.setChecked(!isChecked);
  62. rb4.setChecked(isChecked);
  63. isChecked = false;
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }

Blog地址http://blog.csdn.net/kira012345/article/details/6585125

布局文件:main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal">
  8. <RadioButton android:text="RadioButton1" android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
  9. <RadioButton android:text="RadioButton2" android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
  10. </LinearLayout>
  11. <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="horizontal">
  12. <RadioButton android:text="RadioButton3" android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
  13. <RadioButton android:text="RadioButton4" android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
  14. </LinearLayout>
  15. </LinearLayout>


复制粘贴即可测试。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/733672
推荐阅读
相关标签
  

闽ICP备14008679号