android 中 checkBox 的使用

 

[java]  view plain  copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     // 声明多选列表对象  
  4.     private CheckBox cbx1, cbx2, cbx3, cbx4;  
  5.   
  6.     private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         cbx1 = (CheckBox) findViewById(R.id.checkBox1);  
  14.         cbx2 = (CheckBox) findViewById(R.id.checkBox2);  
  15.         cbx3 = (CheckBox) findViewById(R.id.checkBox3);  
  16.         cbx4 = (CheckBox) findViewById(R.id.checkBox4);  
  17.   
  18.         // 默认选项  
  19.         cbx1.setChecked(true);  
  20.         cbx3.setChecked(true);  
  21.   
  22.           
  23.         cbx1.setOnCheckedChangeListener(listener);  
  24.         cbx2.setOnCheckedChangeListener(listener);  
  25.         cbx3.setOnCheckedChangeListener(listener);  
  26.         cbx4.setOnCheckedChangeListener(listener);  
  27.   
  28.         // 添加到集合中  
  29.         checkBoxs.add(cbx1);  
  30.         checkBoxs.add(cbx2);  
  31.         checkBoxs.add(cbx3);  
  32.         checkBoxs.add(cbx4);  
  33.     }  
  34.   
  35.     @Override  
  36.     public boolean onCreateOptionsMenu(Menu menu) {  
  37.         // Inflate the menu; this adds items to the action bar if it is present.  
  38.         getMenuInflater().inflate(R.menu.main, menu);  
  39.         return true;  
  40.     }  
  41.   
  42.     public void getValues(View v) {  
  43.   
  44.         String content = "";  
  45.   
  46.         for (CheckBox cbx : checkBoxs) {  
  47.             if (cbx.isChecked()) {  
  48.                 content += cbx.getText() + "\n";  
  49.             }  
  50.         }  
  51.   
  52.         if ("".equals(content)) {  
  53.             content = "您还没有选择呢";  
  54.         }  
  55.         new AlertDialog.Builder(this).setMessage(content).setTitle("选中的内容如下")  
  56.                 .setPositiveButton("关闭"null).show();  
  57.   
  58.     }  
  59.   
  60.       
  61.   
  62.     CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {  
  63.   
  64.         @Override  
  65.         public void onCheckedChanged(CompoundButton buttonView,  
  66.                 boolean isChecked) {  
  67.               
  68.             CheckBox box = (CheckBox) buttonView;  
  69.   
  70.             Toast.makeText(getApplicationContext(),  
  71.                     "获取的值:" + isChecked + "xxxxx" + box.getText(),  
  72.                     Toast.LENGTH_LONG).show();  
  73.   
  74.         }  
  75.     };  
  76.   
  77. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值