主题 : android入门之单选按钮(RadioButton) 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-08-28 14:18

 android入门之单选按钮(RadioButton)

复制代码
  1. MainActivity.java
  2. package com.jk.test;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.widget.CompoundButton;
  7. import android.widget.CompoundButton.OnCheckedChangeListener;
  8. import android.widget.RadioButton;
  9. import android.widget.RadioGroup;
  10. public class MainActivity extends Activity {
  11. private RadioButton eatButton;
  12. private RadioButton sleepButton;
  13. private RadioButton dotaButton;
  14. private RadioGroup groupButton;
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. eatButton = (RadioButton) findViewById(R.id.eatButtonid);
  19. sleepButton = (RadioButton) findViewById(R.id.sleepButtonid);
  20. dotaButton = (RadioButton) findViewById(R.id.dotaButtonid);
  21. RadioGroupListener listener1 = new RadioGroupListener();
  22. groupButton.setOnCheckedChangeListener(listener1);
  23. RadioButtonListener listener2 = new RadioButtonListener();
  24. eatButton.setOnCheckedChangeListener(listener2);
  25. sleepButton.setOnCheckedChangeListener(listener2);
  26. dotaButton.setOnCheckedChangeListener(listener2);
  27. }
  28. class RadioButtonListener implements OnCheckedChangeListener{
  29. public void onCheckedChanged(CompoundButton v, boolean isChecked) {
  30. System.out.println(v.getId()+"isChecked--->" + isChecked);
  31. }
  32. }
  33. class RadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener{
  34. public void onCheckedChanged(RadioGroup v, int checkedId) {
  35. if(checkedId == eatButton.getId())
  36. System.out.println("选中了eatButton");
  37. else if(checkedId == sleepButton.getId())
  38. System.out.println("选中了sleepButton");
  39. else if(checkedId == dotaButton.getId())
  40. System.out.println("选中了dotaButton");
  41. }
  42. }
  43. public boolean onCreateOptionsMenu(Menu menu) {
  44. // Inflate the menu; this adds items to the action bar if it is present.
  45. getMenuInflater().inflate(R.menu.main, menu);
  46. return true;
  47. }
  48. }
  49. activity_main.xml
  50. xmlns:tools="http://schemas.android.com/tools"
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:paddingBottom="@dimen/activity_vertical_margin"
  54. android:paddingLeft="@dimen/activity_horizontal_margin"
  55. android:paddingRight="@dimen/activity_horizontal_margin"
  56. android:paddingTop="@dimen/activity_vertical_margin"
  57. tools:context=".MainActivity" >
  58. android:id="@+id/radioGroupId"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:orientation="horizontal">
  62. android:id="@+id/eatButtonid"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:text="吃饭"
  66. />
  67. android:id="@+id/sleepButtonid"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:text="睡觉"
  71. />
  72. android:id="@+id/dotaButtonid"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:text="dota"
  76. />
欢迎加入android开发交流群,群号是:314230976