ToggleButton
16lz
2021-01-23
ToggleButton有两种状态,选中和未选中,即开和关,需要在不同状态下显示不同的文本。
属性:
android:checked="true"
android:textOn="开"
android:textOff="关" 具体文本可以自己设置
新建View:
具体设置:
private ToggleButton tb;
private ImageView img;
//ToggleButton 操作tb= (ToggleButton) findViewById(R.id.tb1);img= (ImageView) findViewById(R.id.iv1);//给tb设置监听器tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //compoundButton 被点击的控件 //b 检测控件状态 img.setImageResource(b?R.drawable.bw1:R.drawable.bw2); }});
同样也可以使用匿名内部类,直接实现接口等方法实现。
如果ImageView设置的是background,则使用img.setBackgroundResourse()
更多相关文章
- android中的基本控件
- Android 日期控件属性
- Android 布局 & 一些控件
- Android UI设计——EditText控件
- 3.EditText控件
- android studio常用控件
- Android图表控件MPAndroidChart——曲线图+柱状图 CombinedChart
- android studio开发 控件布局
- [android]android的容器、控件