Android开发总结笔记 RadioButton和CheckBox(按钮) 1-1-10
来源:互联网 发布:mac 卸载自带输入法 编辑:程序博客网 时间:2024/05/18 02:11
从继承结构来看,CheckBox(API)和RadioButton(API)都是继承于CompoundButton(API)的
根据官方文档的描述,这个CompoundButton是拥有两种可以自动切换状态的按钮
这个特性跟CheckBox和RadioButton一样。
也因为CheckBox和RadioButton都是继承于CompoundButton的,所以这两个子类的大部分方法都在CompoundButton
下面就来看一下他们的常用用法
1、RadioButton
mRgGender= (RadioGroup) findViewById(R.id.rg);
mRgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton= (RadioButton) findViewById(checkedId);
Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show();
}
});
2、CheckBox
public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener {
private CheckBox mCbBasketBall;
private CheckBox mCbBadminton;
private CheckBox mCbPingpong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCbBasketBall = (CheckBox) findViewById(R.id.basketball);
mCbBadminton = (CheckBox) findViewById(R.id.badminton);
mCbPingpong = (CheckBox) findViewById(R.id.pingpong);
mCbBasketBall.setOnCheckedChangeListener(this);
mCbPingpong.setOnCheckedChangeListener(this);
mCbBadminton.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()){
Toast.makeText(this,buttonView.getText(),Toast.LENGTH_SHORT).show();
}
}
}
3、自定义样式
有时候默认的样式可以满足不了我们的需求,所以就需要自定义一些样式
<CheckBox
android:background="@drawable/selector_cb_gender"
android:button="@null"
android:layout_margin="10dp"
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
selector_cb_gender.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_sex_female" android:state_checked="true"></item>
<item android:drawable="@drawable/ic_sex_male" android:state_checked="false"></item>
</selector>
0 0
- Android开发总结笔记 RadioButton和CheckBox(按钮) 1-1-10
- Android开发学习笔记:RadioButton和CheckBox浅析
- Android开发学习笔记:RadioButton和CheckBox浅析
- Android开发学习笔记——浅谈RadioButton和CheckBox
- android学习笔记(三)基础UI组件1——按钮,文本框,CheckBox,Radiobutton介绍
- android学习总结(16.08.29)单选按钮(RadioButton),多选按钮(CheckBox)和开关按钮(Switch)
- android学习笔记12:RadioButton和checkbox
- Android开发之RadioButton和CheckBox
- android开发的CheckBox和RadioButton
- Android CheckBox和RadioButton
- Android-单选按钮RadioButton和复选框checkbox
- Android--(11)--解读单选(RadioButton)和复选(CheckBox)按钮
- android 复选单选按钮 CheckBox和RadioButton
- Android-CheckBox和RadioButton复习
- Android控件CheckBox和RadioButton
- Android RadioButton和CheckBox类
- Android学习笔记(三)checkbox和radiobutton
- Android UI学习笔记 之RadioButton和CheckBox的使用
- Mac搭建hexo指南
- iOS经典讲解之实现App消息推送功能(二)
- AE对象序列化
- 使用Hibernate注解来配置多对一单向的三种方法
- CAS环境下的AJAX跨域
- Android开发总结笔记 RadioButton和CheckBox(按钮) 1-1-10
- 利用 Android Annotations 来玩玩契约编程
- 《深入理解Linux网络技术内幕》阅读笔记(二十)
- UVA - 699 The Falling Leaves
- JavaBean的定义和原理及bean的动作元素
- Scala学习笔记12【继承、抽象类、trait实战】
- MySQL修改root密码的多种方法
- java处理浮点类型的加减乘除法出现小数点精度错乱的解决方法
- Hibernate查询方式汇总