Skip to content

Instantly share code, notes, and snippets.

@li2
Last active March 20, 2018 11:33
Show Gist options
  • Save li2/cd35df67aae1bc678bea32d2444081c1 to your computer and use it in GitHub Desktop.
Save li2/cd35df67aae1bc678bea32d2444081c1 to your computer and use it in GitHub Desktop.
使用 ToggleButton 用来展现两种状态,使用 OnDebouncedClickListener 用来防止多次点击确实起了作用屏蔽了无效点击,但也存在1个问题,快速点击时,ToggleButton 的状态还在切换。如何解决? #tags: android-view
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_menu_stop_rec"/>
<item android:drawable="@drawable/ic_menu_start_rec"/>
</selector>
protected void onCreate(@Nullable Bundle savedInstanceState) {
mOperation1Btn = (ToggleButton) view.findViewById(R.id.operation1);
mOperation1Btn.setOnClickListener(new OnDebouncedClickListener() {
@Override
public void onDebouncedClick(View v) {
onToolbarOperation1Clicked(mOperation1Btn.isChecked());
}
});
}
protected abstract void onToolbarOperation1Clicked(boolean isChecked);
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:elevation="@dimen/elevation_header">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:paddingLeft="@dimen/activity_horizontal_padding"
android:paddingRight="@dimen/activity_horizontal_padding"
android:background="@color/toolbar_background"
app:theme="@style/Dvr.Toolbar"
app:popupTheme="@style/Dvr.PopupOverlay">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ToggleButton
android:id="@+id/operation1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left|center_vertical"
android:textOn="@string/action_stop_recording"
android:textOff="@string/action_start_recording"
android:drawableLeft="@drawable/state_list_toolbar_recording"
android:checked="true"/>
<ToggleButton
android:id="@+id/operation2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical"
android:textOn="@string/action_camera"
android:textOff="@string/action_camera"
android:drawableLeft="@drawable/ic_menu_camera"
android:checked="true"/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment