android学习日记03--常用控件checkbox/radiobutton

常用控件
3checkbox

 1 package com.example.checkbox;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.widget.CheckBox;
 7 import android.widget.CompoundButton;
 8 import android.widget.Toast;
 9 import android.widget.CompoundButton.OnCheckedChangeListener;
10 import android.widget.TextView;
11 
12 public class MainActivity extends Activity implements OnCheckedChangeListener{
13 
14     private CheckBox cb1,cb2,cb3;
15     private TextView tv;
16     
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         
22         cb1 = (CheckBox)findViewById(R.id.cb1);
23         cb2 = (CheckBox)findViewById(R.id.cb2);
24         cb3 = (CheckBox)findViewById(R.id.cb3);
25         
26         cb1.setOnCheckedChangeListener(this);
27         cb2.setOnCheckedChangeListener(this);
28         cb3.setOnCheckedChangeListener(this);
29         
30         tv = (TextView)findViewById(R.id.tv);
31     }
32 
33 
34     @Override
35     public boolean onCreateOptionsMenu(Menu menu) {
36         // Inflate the menu; this adds items to the action bar if it is present.
37         getMenuInflater().inflate(R.menu.main, menu);
38         return true;
39     }
40 
41 
42     @Override
43     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
44         // TODO Auto-generated method stub
45         if(cb1 == buttonView||cb2 == buttonView||cb3 == buttonView) {
46             if(isChecked) {
47                 showToast(buttonView.getText()+"选中");
48             } else {
49                 showToast(buttonView.getText()+"取消");
50             }
51         }
52             
53     }
54     
55     public void showToast(String str) {
56         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
57     }
58     
59 }
View Code

复选框,确定是否勾选,点击一下勾选,点击第二下取消

用法同前面的button,不再赘述,代码新增
Toast.makeText(this, st, Toast.LENGTH_SHORT);

Toast译为土司类似切片面包用于弹出提示信息

效果如下:

爱好有很多种,可以同时选足球和网球

4、radiobutton

radiobutton 与checkbox对应
区别是rb是一组选择框,只能选择其中一个,而且radiobutton一般与RadioGroup一起用

 1 package com.example.radiobutton;
 2 
 3 
 4 import android.os.Bundle;
 5 import android.app.Activity;
 6 import android.view.Menu;
 7 import android.widget.RadioButton;
 8 import android.widget.RadioGroup;
 9 import android.widget.TextView;
10 import android.widget.Toast;
11 import android.widget.RadioGroup.OnCheckedChangeListener;
12 
13 public class MainActivity extends Activity implements OnCheckedChangeListener{
14 
15     private RadioGroup rg;
16     private RadioButton rb1,rb2;
17     private TextView tv;
18     
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         
24         rg = (RadioGroup)findViewById(R.id.rg);
25         rb1 = (RadioButton)findViewById(R.id.rb1);
26         rb2 = (RadioButton)findViewById(R.id.rb2);
27         tv = (TextView)findViewById(R.id.tv);
28         
29         rg.setOnCheckedChangeListener(this);
30     }
31 
32 
33     @Override
34     public boolean onCreateOptionsMenu(Menu menu) {
35         // Inflate the menu; this adds items to the action bar if it is present.
36         getMenuInflater().inflate(R.menu.main, menu);
37         return true;
38     }
39 
40 
41     @Override
42     public void onCheckedChanged(RadioGroup group, int checkedId) {
43         // TODO Auto-generated method stub
44         if (group == rg) {
45             String rbstr = null;
46             if (checkedId == rb1.getId()) {
47                 rbstr = rb1.getText()+"选中";
48                 //tv.setText(rbstr);
49             }else if(checkedId == rb2.getId()) {
50                 rbstr = rb2.getText()+"选中";
51                 //tv.setText(rbstr);
52             }
53             showToast(rbstr);
54         }
55     }
56     
57     public void showToast(String str) {
58         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
59     }
60     
61 }
View Code

效果如下:

性别只能选男女中的一个。用单选框,选radiobutton。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。