http://www.beijibear.com/index.php?aid=336

android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果,运行效果截图如下:

CheckBox控件的定义,main.xml内容如下:

  
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <EditText
  8. android:id="@+id/editText1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="请选择"
  12. />
  13. <CheckBox
  14. android:id="@+id/beijing"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="北京"
  18. />
  19. <CheckBox
  20. android:id="@+id/shanghai"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="上海"
  24. />
  25. <CheckBox
  26. android:id="@+id/shenzhen"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="深圳"
  30. />
  31. </LinearLayout>

activity CheckBoxTest.java内容如下:

  
  1. packagecheckbox.pack;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.CheckBox;
  5. importandroid.widget.CompoundButton;
  6. importandroid.widget.EditText;
  7. publicclassCheckBoxTestextendsActivity{
  8. //对控件对象进行声明
  9. CheckBoxbeijing=null;
  10. CheckBoxshanghai=null;
  11. CheckBoxshenzhen=null;
  12. EditTexteditText1=null;
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. //通过控件的ID来得到代表控件的对象
  18. beijing=(CheckBox)findViewById(R.id.beijing);
  19. shanghai=(CheckBox)findViewById(R.id.shanghai);
  20. shenzhen=(CheckBox)findViewById(R.id.shenzhen);
  21. editText1=(EditText)findViewById(R.id.editText1);
  22. //给CheckBox设置事件监听
  23. beijing.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  24. @Override
  25. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  26. booleanisChecked){
  27. //TODOAuto-generatedmethodstub
  28. if(isChecked){
  29. editText1.setText(buttonView.getText()+"选中");
  30. }else{
  31. editText1.setText(buttonView.getText()+"取消选中");
  32. }
  33. }
  34. });
  35. shanghai.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  36. @Override
  37. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  38. booleanisChecked){
  39. //TODOAuto-generatedmethodstub
  40. if(isChecked){
  41. editText1.setText(buttonView.getText()+"选中");
  42. }else{
  43. editText1.setText(buttonView.getText()+"取消选中");
  44. }
  45. }
  46. });
  47. shenzhen.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  48. @Override
  49. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  50. booleanisChecked){
  51. //TODOAuto-generatedmethodstub
  52. if(isChecked){
  53. editText1.setText(buttonView.getText()+"选中");
  54. }else{
  55. editText1.setText(buttonView.getText()+"取消选中");
  56. }
  57. }
  58. });
  59. }
  60. }

http://www.beijibear.com/index.php?aid=336

android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果,运行效果截图如下:

CheckBox控件的定义,main.xml内容如下:

   
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <EditText
  8. android:id="@+id/editText1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="请选择"
  12. />
  13. <CheckBox
  14. android:id="@+id/beijing"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="北京"
  18. />
  19. <CheckBox
  20. android:id="@+id/shanghai"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="上海"
  24. />
  25. <CheckBox
  26. android:id="@+id/shenzhen"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="深圳"
  30. />
  31. </LinearLayout>

activity CheckBoxTest.java内容如下:

   
  1. packagecheckbox.pack;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.CheckBox;
  5. importandroid.widget.CompoundButton;
  6. importandroid.widget.EditText;
  7. publicclassCheckBoxTestextendsActivity{
  8. //对控件对象进行声明
  9. CheckBoxbeijing=null;
  10. CheckBoxshanghai=null;
  11. CheckBoxshenzhen=null;
  12. EditTexteditText1=null;
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. //通过控件的ID来得到代表控件的对象
  18. beijing=(CheckBox)findViewById(R.id.beijing);
  19. shanghai=(CheckBox)findViewById(R.id.shanghai);
  20. shenzhen=(CheckBox)findViewById(R.id.shenzhen);
  21. editText1=(EditText)findViewById(R.id.editText1);
  22. //给CheckBox设置事件监听
  23. beijing.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  24. @Override
  25. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  26. booleanisChecked){
  27. //TODOAuto-generatedmethodstub
  28. if(isChecked){
  29. editText1.setText(buttonView.getText()+"选中");
  30. }else{
  31. editText1.setText(buttonView.getText()+"取消选中");
  32. }
  33. }
  34. });
  35. shanghai.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  36. @Override
  37. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  38. booleanisChecked){
  39. //TODOAuto-generatedmethodstub
  40. if(isChecked){
  41. editText1.setText(buttonView.getText()+"选中");
  42. }else{
  43. editText1.setText(buttonView.getText()+"取消选中");
  44. }
  45. }
  46. });
  47. shenzhen.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  48. @Override
  49. publicvoidonCheckedChanged(CompoundButtonbuttonView,
  50. booleanisChecked){
  51. //TODOAuto-generatedmethodstub
  52. if(isChecked){
  53. editText1.setText(buttonView.getText()+"选中");
  54. }else{
  55. editText1.setText(buttonView.getText()+"取消选中");
  56. }
  57. }
  58. });
  59. }
  60. }

更多相关文章

  1. Andorid TabHost 使用小结
  2. Android小问题:android studio怎么查看数字签名 sha1(集成百度地图
  3. Android(安卓)开发 短信app
  4. Android(安卓)GridView控件 使用
  5. android:TimePicker仿照IOS时间选择器,可自定义选择器
  6. 推荐4款开源的Android引导页控件
  7. android中json解析的两个工具:Gson和Jackson的使用小demo
  8. 完美解决 Failed to fetch URL https://dl-ssl.google.com/andro
  9. android 进阶自定义控件1

随机推荐

  1. Android 利用adb命令 使App自动点击屏幕
  2. Android手电筒小程序实现代码
  3. Android OpenGL 3D游戏开发入门必看文章
  4. Android OnSensorChanged() not working
  5. Android和PC端通过局域网文件同步
  6. Android——Notifications笔记
  7. Android fragment在xml中使用没添加ID
  8. android ListView 的使用
  9. Android 给View加圆角
  10. Android :java.lang.RuntimeException: t