1.重写监听事件
public class OnLimitClickHelper implements View.OnClickListener {
public static final int LIMIT_TIME = 300;
private long lastClickTime = 0;
private OnLimitClickListener onLimitClickListener = null;
public OnLimitClickHelper(OnLimitClickListener onLimitClickListener) {
this.onLimitClickListener = onLimitClickListener;
}
@Override
public void onClick(View v) {
long curTime = Calendar.getInstance().getTimeInMillis();
if (curTime - lastClickTime > LIMIT_TIME) {
lastClickTime = curTime;
if (onLimitClickListener != null) {
onLimitClickListener.onClick(v);
}
}
}
}
2.监听接口
public interface OnLimitClickListener {
/**
* 按钮点击事件
* @param view
*/
void onClick(View view);
}
3.使用
public abstract class MainActivity extends AppCompatActivity implements OnLimitClickListener {
private Button btn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new OnLimitClickHelper(this));
}
@Override
public void onClick(View v) { //TODO } }
---------------------
@Override
public float getSizeInDp () {
return 640;
}
}