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

常用控件
3、checkbox

  复选框,确定是否勾选,点击一下勾选,点击第二下取消,当有一系列备选项时适合用checkbox控件,方便用户提交数据。

贴上例子Activity的java代码

 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译为土司,类似切片面包,用于弹出提示信息

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,

而且Toast显示的时间有限,过一定的时间就会自动消失,后面有机会再详细介绍Toast(这里有介绍:android学习日记21--消息提示之Toast和Notification)

效果如下:

"网球选中"就是Toast的弹出消息,爱好有很多种,可以同时选足球和网球。

4、radiobutton

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

而且rb一般是圆形的,checkbox是方形的;不同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。

========个人网站:http://chendd.com/ 文章很多是上面搬过来的,以后都在个人网站上更新,有兴趣的可以移步☺========
原文地址:https://www.cnblogs.com/aiguozhe/p/3543570.html