<b>一、 </b><b>CheckBox</b><b>控件</b>
布局文件代码:
<CheckBoxandroid:id="@+id/read"android:text="阅读" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBoxandroid:id="@+id/game"android:text="游戏" android:layout_width="fill_parent"
<CheckBoxandroid:id="@+id/movie"android:text="电影" android:layout_width="fill_parent"
Activity代码:
//设置监听器
read=(CheckBox)findViewById(R.id.read);
read.setOnCheckedChangeListener(<b>new</b> CompoundButton.OnCheckedChangeListener() {
<b>public</b> <b>void</b> onCheckedChanged(CompoundButton buttonView, <b>boolean</b> isChecked) {
// <b>TODO</b> Auto-generated method stub
<b>if</b>(read.isChecked()){
Toast.makeText(CheckDemo.<b>this</b>, "你选择了:"+read.getText(), Toast.LENGTH_LONG).show();
}
}
});
<b>二、 </b><b>RadioButton</b><b>控件</b>
RadioButton控件必须位于一个RadioGroup内。
布局代码如下:
<RadioGroupandroid:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButtonandroid:id="@+id/nan"android:text="男"android:layout_width="fill_parent"
<RadioButtonandroid:id="@+id/nv"android:text="女"android:layout_width="fill_parent"
</RadioGroup>
Activity代码如下:
rg=(RadioGroup)findViewById(R.id.rg);
rg.setOnCheckedChangeListener(<b>new</b> RadioGroup.OnCheckedChangeListener() {
<b>public</b> <b>void</b> onCheckedChanged(RadioGroup group, <b>int</b> checkedId) {
<b>if</b>(nan.getId()==checkedId){
Toast.makeText(CheckDemo.<b>this</b>, "你选择了:"+nan.getText().toString(), Toast.LENGTH_SHORT).show();
<b>if</b>(nv.getId()==checkedId){
Toast.makeText(CheckDemo.<b>this</b>, "你选择了:"+nv.getText().toString(), Toast.LENGTH_SHORT).show();
<b>三、 </b><b>Toast</b><b>的用法</b>
Toast.makeText(context,stringInfo,Toast. LISTEN_LONG).show();
Context指明了当前上下文,一般是当前activity的类