ToggleButton

来源:互联网 发布:linux如何复制命令行 编辑:程序博客网 时间:2024/05/29 14:22

ToggleButton有两种状态,选中和未选中,即开和关,需要在不同状态下显示不同的文本。


属性:

android:checked="true"

android:textOn="开"

android:textOff="关"    具体文本可以自己设置


新建View:

<ToggleButton    android:id="@+id/tb1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:textOn="开"    android:textOff="关"    />


<ImageView    android:id="@+id/iv1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:scaleType="centerCrop"    android:src="@drawable/bw2"/>

具体设置:

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()

0 0