【按住你的心】——Android开发CheckBox&RadioButton控件的简单使用

  Android的控件很多,我们从最常用的一些控件学起,今天我们学习CheckBox和RadioButton。

  首先,我们要在main.xml文件中添加控件:  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />

        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male" />
    </RadioGroup>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/car" />

</LinearLayout>

  这里的RadioButton是在RadioGroup下的两个单选项,我们知道在应用中,单选按钮通常都是只能选一个,所以你可以设置一个RadioGroup中有几个单选按钮,况且在运行的时候在该组下你只能选择一项。

  对应的string.xml文件: 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, CheckBox_RadioButtonActivity!</string>
    <string name="app_name">CheckBox_RadioButton</string>
    <string name="female"></string>
    <string name="male"></string>
    <string name="car">汽车</string>
</resources>

  剩下我们所要做的事情就是在Activity文件中为单选和多选按钮添加监听了,源码如下:

public class CheckBox_RadioButtonActivity extends Activity implements
        OnCheckedChangeListener {
    private RadioButton female;
    private RadioButton male;
    private CheckBox cb;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取CheckBox对象
        cb = (CheckBox) findViewById(R.id.checkbox);
        // 获取两个RadioButton对象
        female = (RadioButton) findViewById(R.id.female);
        male = (RadioButton) findViewById(R.id.male);
        
        // 为CheckBox添加监听
        cb.setOnCheckedChangeListener(this);
        // 为RadioButton添加监听
        female.setOnClickListener(radio_listener);
        male.setOnClickListener(radio_listener);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // 对CheckBox的选中状态进行判断并且作出相应的操作
        if (isChecked == true) {
            Toast.makeText(getApplicationContext(), cb.getText(),
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "you choose cancel",
                    Toast.LENGTH_LONG).show();
        }
    }

    private OnClickListener radio_listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 在RadionButton选中时所要做的操作
            RadioButton rb = (RadioButton) v;
            Toast.makeText(getApplicationContext(), rb.getText(),
                    Toast.LENGTH_SHORT).show();
        }
    };
}

  这里要实现OnCheckedChangeListener这个接口,因为CheckBox的监听器是从CompoundButton继承下来的,所以这里不同于其它控件可以使用回调函数为自身添加监听。

  PS:有的时候我们用Eclipse的Alt+?提示功能出不来OnCheckedChangeListener的提示,大家可以在文件头部手动导入下面两个包:

import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

  之后就会有相应的提示了。

  下面我们看运行结果:

  1.初始化效果:

  

  2.点击RadionButton男或者女时:

  

  3.选中CheckBox时:

  

  4.取消选中时:

  

  关于点击后Toast的提示我们以后会进行深入的学习。

posted @ 2012-05-25 16:35  Laughing_Vzr@Stand By  阅读(2749)  评论(0编辑  收藏  举报