android ToggleButton
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。
XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/toggleButton1"
android:text="TextView" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:textOn="你好"
android:textOff="你不好"
android:text="ToggleButton" />
</RelativeLayout>
toggleButton = (ToggleButton) this.findViewById(R.id.toggleButton1);
view = (TextView) this.findViewById(R.id.textView1);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
toggleButton.setChecked(isChecked);
view.setText(isChecked?"对":"错");
}
});
浙公网安备 33010602011771号