public void onToggleClicked(View view) {
// Is the toggle on?
switch (view.getId()) {
case R.id.xyz:
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Toast.makeText(mContext, "A", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "B", Toast.LENGTH_SHORT).show();
}
}
}
切換按鈕
的使用屬性在XML:
<ToggleButton
android:id="@+id/xyz"
style="@style/toggleButton"
android:layout_width="190dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="8dp"
android:background="@drawable/ic_toggle_sv"
android:onClick="onToggleClicked" />
在ic_toggle_sv
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+android:id/background"
android:drawable="@android:color/transparent"/>
<item
android:id="@+android:id/toggle"
android:drawable="@drawable/ic_toggle1"/>
</layer-list>
在ic_toggle1通過切換按鈕
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_toggle_stills" android:state_checked="false"/>
<item android:drawable="@drawable/ic_toggle_videos" android:state_checked="true"/>
</selector>
切換?像複選框? – Blackbelt
like toggleButton – user3775061