android 无法阻止多次按钮点击
Unable to prevent multiple button clicks with android
目前我正在制作一个简单的 android 应用程序,但我遇到了一个问题,我被困了一段时间。我在 google 上阅读了很多关于如何使用 android 防止多次点击的文章,但是,当我按下按钮时没有任何动作发生。
我想做的就是防止用户多次点击。我已经发布了示例代码。请问有没有错误...
private long mLastClickTime = 0;
public void sendData(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
} else {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
//sending the data
trySendingData trying = new trySendingData();
trying.execute();
}
}
我要做的是执行asyncTask trying.execute();单击一下。
就说
Whateverbuttoncallssenddata.setEnabled(false);
或
Whateverbuttoncallssenddata.buttsetOnClickListener(null);
只要不是文本视图即可。
您可以在用户点击后禁用按钮点击 btnSendData.setEnabled(false)
,开始执行带有要执行任务的异步任务。任务完成后,您可以启用按钮单击 asynctask 的 onPostExecute() 方法,或者您可以将结果返回到您想要的位置,然后通过调用 btnSendData.setEnabled(true)
再次启用按钮单击
有多种方法可以实现:
方法一:
设置标签。
像这样创建变量
Boolean isClicked = false;
一旦你的代码达到这个
button.setonclick...{
if (!isClicked){
isClicked = true;
//Write your AsynkTask here
}
}
方法二:
单击后禁用按钮
例如:
button.setonclick...{
button.setEnabled(false);
//Write your AsynkTask here
}
}
注:
您正在从 xml 调用 clcik 操作。你需要这样修改:
在这种方法下:
public void sendData(View v) {
v.setEnabled(false);
}
我认为第二个是你需要的。我添加方法 1 以获取信息
可以在onClickListener中调用该方法
public static void blockView(final View v) {
v.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
v.setEnabled(true);
}
}, 1000);
}
这将阻止视图 1 秒
正如 Piyush 和 SmulianJulian 所说,禁用单击,如果启用它,则在异步任务结束时启用它。像这样:
public void sendData(View v) {
//First disable button so user cannot click it immediately
v.setEnable(false);
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
v.setEnable(true);//This makes view clickable
} else {
//sending the data
trySendingData trying = new trySendingData()
{
@Override
protected void onPostExecute(View result) {
//This makes view clickable after completing your asyncTask trySendingData
v.setEnable(true);
super.onPostExecute(result);
}
};
trying.execute();
}
}
目前我正在制作一个简单的 android 应用程序,但我遇到了一个问题,我被困了一段时间。我在 google 上阅读了很多关于如何使用 android 防止多次点击的文章,但是,当我按下按钮时没有任何动作发生。 我想做的就是防止用户多次点击。我已经发布了示例代码。请问有没有错误...
private long mLastClickTime = 0;
public void sendData(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
} else {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
//sending the data
trySendingData trying = new trySendingData();
trying.execute();
}
}
我要做的是执行asyncTask trying.execute();单击一下。
就说
Whateverbuttoncallssenddata.setEnabled(false);
或
Whateverbuttoncallssenddata.buttsetOnClickListener(null);
只要不是文本视图即可。
您可以在用户点击后禁用按钮点击 btnSendData.setEnabled(false)
,开始执行带有要执行任务的异步任务。任务完成后,您可以启用按钮单击 asynctask 的 onPostExecute() 方法,或者您可以将结果返回到您想要的位置,然后通过调用 btnSendData.setEnabled(true)
有多种方法可以实现:
方法一:
设置标签。
像这样创建变量
Boolean isClicked = false;
一旦你的代码达到这个
button.setonclick...{
if (!isClicked){
isClicked = true;
//Write your AsynkTask here
}
}
方法二:
单击后禁用按钮 例如:
button.setonclick...{
button.setEnabled(false);
//Write your AsynkTask here
}
}
注: 您正在从 xml 调用 clcik 操作。你需要这样修改: 在这种方法下:
public void sendData(View v) {
v.setEnabled(false);
}
我认为第二个是你需要的。我添加方法 1 以获取信息
可以在onClickListener中调用该方法
public static void blockView(final View v) {
v.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
v.setEnabled(true);
}
}, 1000);
}
这将阻止视图 1 秒
正如 Piyush 和 SmulianJulian 所说,禁用单击,如果启用它,则在异步任务结束时启用它。像这样:
public void sendData(View v) {
//First disable button so user cannot click it immediately
v.setEnable(false);
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
v.setEnable(true);//This makes view clickable
} else {
//sending the data
trySendingData trying = new trySendingData()
{
@Override
protected void onPostExecute(View result) {
//This makes view clickable after completing your asyncTask trySendingData
v.setEnable(true);
super.onPostExecute(result);
}
};
trying.execute();
}
}