Chendd's Blog

世界上没有什么事情是一行代码不能解决的。如果有,那就两行。

Android学习日记3--常用控件checkbox/radiobutton

常用控件

3、checkbox

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

贴上例子Activity的java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener{

    private CheckBox cb1,cb2,cb3;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cb1 = (CheckBox)findViewById(R.id.cb1);
        cb2 = (CheckBox)findViewById(R.id.cb2);
        cb3 = (CheckBox)findViewById(R.id.cb3);

        cb1.setOnCheckedChangeListener(this);
        cb2.setOnCheckedChangeListener(this);
        cb3.setOnCheckedChangeListener(this);

        tv = (TextView)findViewById(R.id.tv);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(cb1 == buttonView||cb2 == buttonView||cb3 == buttonView) {
            if(isChecked) {
                showToast(buttonView.getText()+"选中");
            } else {
                showToast(buttonView.getText()+"取消");
            }
        }

    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

}

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

Toast译为土司,类似切片面包,用于弹出提示信息,Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失,后面有机会再详细介绍Toast(这里有介绍:android学习日记21–消息提示之Toast和Notification)

效果如下:

image

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

4、radiobutton

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

而且rb一般是圆形的,checkbox是方形的;不同RadioGroup互不相干,一般有两个或两个以上的选项,并且有默认值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.radiobutton;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener{

    private RadioGroup rg;
    private RadioButton rb1,rb2;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup)findViewById(R.id.rg);
        rb1 = (RadioButton)findViewById(R.id.rb1);
        rb2 = (RadioButton)findViewById(R.id.rb2);
        tv = (TextView)findViewById(R.id.tv);

        rg.setOnCheckedChangeListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if (group == rg) {
            String rbstr = null;
            if (checkedId == rb1.getId()) {
                rbstr = rb1.getText()+"选中";
                //tv.setText(rbstr);
            }else if(checkedId == rb2.getId()) {
                rbstr = rb2.getText()+"选中";
                //tv.setText(rbstr);
            }
            showToast(rbstr);
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

}

效果如下:

image

       性别只能选男女中的一个(不考虑人妖哈!)。最好用单选框,选radiobutton。

Comments