[安卓] 4、CheckBox、RadioButton和Toast简单用法

时间:2024-04-06 00:04:35

[安卓] 4、CheckBox、RadioButton和Toast简单用法 [安卓] 4、CheckBox、RadioButton和Toast简单用法


和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChanged抽象函数中对点击CheckBox的状态进行获取并用Toast显示。

 //使用状态改变检查监听器
public class MainActivity extends Activity implements OnCheckedChangeListener {
private CheckBox cb1, cb2, cb3;//创建3个CheckBox对象 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化3个CheckBox
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);
} //重写监听器的抽象函数
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//buttonView 选中状态发生改变的那个按钮
//isChecked 表示按钮新的状态(true/false)
if (cb1 == buttonView || cb2 == buttonView || cb3 == buttonView) {
if (isChecked) {
//显示一个提示信息
toastDisplay(buttonView.getText() + "选中"); } else {
toastDisplay(buttonView.getText() + "取消选中");
}
}
} public void toastDisplay(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}

了解上述用法之后,我们来看一下radioButton的用法:注意这里不同的地方是第13、14行,没有像CheckBox一样每一个控件绑定CheckChange监听器,而是将RadioGroup进行绑定。

 //使用状态改变监听器
public class MainActivity extends Activity implements OnCheckedChangeListener {
private RadioButton rb1, rb2, rb3;
private RadioGroup rg; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rg = (RadioGroup) findViewById(R.id.radGrp);
rg.setOnCheckedChangeListener(this);//将单选组绑定监听器
} //重写监听器函数
/**
* @param group:指单选组
* @param group:指单选组中发生状态改变的RadioButton的内存ID!
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group == rg) {//因为当前程序中只有一个RadioGroup,此步可以不进行判定
String rbName = null;
if (checkedId == rb1.getId()) {
rbName = rb1.getText().toString();
} else if (checkedId == rb2.getId()) {
rbName = rb2.getText().toString();
} else if (checkedId == rb3.getId()) {
rbName = rb3.getText().toString();
}
Toast.makeText(this, "选择了下标为“" + rbName + "”的单选按钮",
Toast.LENGTH_LONG).show();
}
}
}

那这个RadioGroup是怎么和RadioButton结合在一起呈现多选一的效果的呢?我们来看看xml就知道了:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:id="@+id/radGrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton1"
android:id="@+id/rb1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton2"
android:id="@+id/rb2"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton3"
android:id="@+id/rb3"
/>
</RadioGroup>
</LinearLayout>

本文链接:http://www.cnblogs.com/zjutlitao/p/4229767.html

更多精彩:http://www.cnblogs.com/zjutlitao/