新建OnOneOffClickListener.java
public abstract class OnOneOffClickListener implements View.OnClickListener {
private boolean clickable = true;
/**
* Override onOneClick() instead.
*/
@Override
public void onClick(View v) {
if (clickable) {
clickable = false;
onOneClick(v);
//reset(); // uncomment this line to reset automatically
}
}
/**
* Override this function to handle clicks.
* reset() must be called after each click for this function to be called
* again.
* @param v
*/
public abstract void onOneClick(View v);
/**
* Allows another click.
*/
public void reset() {
clickable = true;
}
}
xml页面文件
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="orderHandler"
type="com.***.OnOneOffClickListener" />
</data>
........
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:onClick="@{orderHandler::onClick}"
android:text="@string/commit_buy" />
........
</layout>
Activity/Fragment文件
mBinding.setOrderHandler(new OnOneOffClickListener() {
@Override
public void onOneClick(View v) {
....
reset();
....
}
});