1.CheckBox 复选框
【
继承自CompoundButton(带选择状态的按钮)
CheckBox继承于Button,所以具有普通按钮的各种属性,但是与普通按钮不同的是,CheckBox
提供了可选中的功能。
《
private class MyListener implements OnCheckedChangeListener{
@Override
publicvoid onCheckedChanged(CompoundButton buttonView,
booleanisChecked) {
switch(buttonView.getId()) {
caseR.id.cbox_color:
if(isChecked){
tv.setTextColor(Color.RED);
}else{
tv.setTextColor(Color.BLACK);
}
break;
}
}
cbox_color.setOnCheckedChangeListener(newMyListener());
》
】
代码练习:
【
publicclass MainActivity extends Activity implements OnCheckedChangeListener {
private TextView show_text;
private CheckBox cb1, cb2, cb3, cb_all;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate
(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initListener();
}
private void initListener() {
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener
(this);
//为了避免选中状态处理的冲突,我们只需要考虑点击“全选”按钮时的业务逻辑
cb_all.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
boolean checked = cb_all.isChecked();
cb1.setChecked(checked);
cb2.setChecked(checked);
cb3.setChecked(checked);
}
});
}
private void findViews() {
show_text = (TextView)this.findViewById(R.id.show_text);
cb1 = (CheckBox)this.findViewById(R.id.cb1);
cb2 = (CheckBox)this.findViewById(R.id.cb2);
cb3 = (CheckBox)this.findViewById(R.id.cb3);
cb_all = (CheckBox)this.findViewById(R.id.cb_all);
}
@Override
public voidonCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//根据当前三个复选框的选中状态修改“全选/取消全选”的选中状态
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()){
cb_all.setChecked(true);
}else{
cb_all.setChecked(false);
}
updateText();
}
private void updateText() {
//
show_text.setText("");
//逐个判断三个复选框的选中状态,显示被选中的
if(cb1.isChecked()){
// show_text.setText(cb1.getText()+"");
show_text.append(cb1.getText()+"");
}
if(cb2.isChecked()){
// show_text.setText(cb2.getText()+"");
show_text.append(cb2.getText()+" "); }
if(cb3.isChecked()){
// show_text.setText(cb3.getText()+"");
show_text.append(cb3.getText()+"");
}
}
}
】
2.RadioButton、RadioGroup 单选按钮(组)
【
==RadioButton继承于Button,所以具有普通按钮的各种属性,但是与普通按钮不同的是,在使
用RadioButton的时候,要使用RadioGroup来包围起RadioButton
==RadioGroup是LinearLayout的子类,所以RadioGroup本质上是一个存放RadioButton的布局容
器
==默认的LinearLayout布局的Orientation属性是水平的,而默认的RadioGroup的Orientation
属性是垂直的
==RadioGroup类中的getCheckedRadioButtonId()根据id获得RadioGroup里指定的RadioButton
?
示例说明:
radio_group = (RadioGroup) this.findViewById(R.id.radio_group);
radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
/**
* 当RadioGroup中的单选按钮被选择时调用的方法
-
*/ @Override public voidonCheckedChanged(RadioGroup group, intcheckedId) { switch (checkedId) { case R.id.ra_male://男 Toast.makeText(MainActivity.this, "你的选择是,男",
0).show();
break;
case R.id.ra_female://女
Toast.makeText(MainActivity.this, "你的选择是,女",
0).show();
break;
default:
break;
}
}
});
switch (radio_group.getCheckedRadioButtonId()){
case R.id.ra_male:
weiht= (in_height-80)*0.7;
break;
case R.id.ra_female:
weiht= (in_height-70)*0.6;
break;
default:
break;
}
】
导航效果相关
【
<RadioButton
android:id="@+id/radio_data"
android:drawableTop="@drawable/slt_data"
android:text=“DATA”
style="@style/radiobutton_style"/>
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/radio_select"
android:clickable="false"
android:checked=“true”/>
@Override
public void onPageSelected(intpotion) {
// TODOAuto-generated method stub
//获取应该被选中的radiobutton对象,getChildAt方法
:获取radiogroup中位于position位置的子控件对象
RadioButton rb =(RadioButton) rGroup.getChildAt
(potion);
//设置让参数中指定的id对应RadioButton对象变为选中
效果
rGroup.check(rb.getId());
}
】