实例详解Android解决按钮重复点击问题

时间:2022-03-02 09:31:40

 为了防止用户或者测试MM疯狂的点击某个button,写个方法防止按钮连续点击。具体实例代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class BaseActivity extends Activity {
protected boolean isDestroy;
//防止重复点击设置的标志,涉及到点击打开其他Activity时,将该标志设置为false,在onResume事件中设置为true
private boolean clickable=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isDestroy=false;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
protected void onDestroy() {
super.onDestroy();
isDestroy=true;
}
@Override
protected void onResume() {
super.onResume();
//每次返回界面时,将点击标志设置为可点击
clickable=true;
}
/**
* 当前是否可以点击
* @return
*/
protected boolean isClickable(){
return clickable;
}
/**
* 锁定点击
*/
protected void lockClick(){
clickable=false;
}
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if(isClickable()) {
lockClick();
super.startActivityForResult(intent, requestCode,options);
}
}
}

通过一段简单的代码给大家介绍了Android解决按钮重复点击问题,希望大家喜欢。