CheckBox标签和属性

CheckBox的作用:可以提供复选

下面是我点击按钮查看所选内容的代码:定义按钮监听器,并在onClick方法中调用shoeAlt方法(此方法会在第二块代码定义)

 1 Button btn=(Button) findViewById(R.id.btn);
 2         
 3         btn.setOnClickListener(new View.OnClickListener() {
 4             
 5             @Override
 6             public void onClick(View v) {
 7                 showAlt();
 8                 
 9             }
10         });

接下来是定义showAlt方法(首先初始化控件,然后用isCheck方法查看,这些控件哪些被选中,如果被选中就用getText和toString方法得到控件的text)

到这里不得不说Toast

Toast给当前视图显示一个浮动的显示块,它永远不会获得焦点,我会在下一篇的博客中详细解释Toast

 

 1 protected CheckBox run, sleep, swim, eat, playball;
 2 
 3     protected void showAlt() {
 4         run = (CheckBox) findViewById(R.id.run);
 5         sleep = (CheckBox) findViewById(R.id.sleep);
 6         swim = (CheckBox) findViewById(R.id.swim);
 7         eat = (CheckBox) findViewById(R.id.eat);
 8         playball = (CheckBox) findViewById(R.id.playball);
 9         StringBuffer str = new StringBuffer();
10         if (run.isChecked()) {
11             str.append(run.getText().toString() + ",");
12         }
13         if (sleep.isChecked()) {
14             str.append(sleep.getText().toString() + ",");
15         }
16         if (swim.isChecked()) {
17             str.append(swim.getText().toString() + ",");
18         }
19         if (eat.isChecked()) {
20             str.append(eat.getText().toString() + ",");
21         }
22         if (playball.isChecked()) {
23             str.append(playball.getText().toString() + ",");
24         }
25         if (str.length() == 0) {
26             Toast.makeText(this, "请选择您的爱好", 1).show();
27         } else {
28             String str1 = str.substring(0, str.length() - 1);
29             Toast.makeText(this, "你的爱好是:" + str1, 1).show();
30         }
31 
32     }

 

posted @ 2020-02-03 11:38  东功  阅读(3241)  评论(0编辑  收藏  举报