英文:
Android Preventing Double Click On A Layout with handher
问题
以下是翻译好的内容:
有一个包含进度条的按钮布局。当点击按钮时,它会暂停几秒钟,然后进入下一个活动。
为了防止双重点击,我认为我需要将这些代码放在 OnclickListener 中:
private long mLastClickTime = 0;
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
但我不知道放在哪里。
我的OnClickListener方法:
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
progress.setVisibility(View.GONE);
Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
startActivity(intent);
}
}, 4500);
}
});
我将监听器更改为:
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
progress.setVisibility(View.GONE);
Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
startActivity(intent);
}
}, 4500);
mLastClickTime = SystemClock.elapsedRealtime();
}
});
但是,当我多次点击按钮时,它会多次打开下一个活动。
英文:
There is a button layout that also has a progress bar inside. When the button is clicked, it pauses for a few seconds and enters the next activity.
To prevent double click i think i have to put these codes on OnclickListener
private long mLastClickTime = 0;
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
But i don't no where
My onClickListener method:
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
progress.setVisibility(View.GONE);
Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
startActivity(intent);
}
}, 4500);
}
});
I change my listener to :
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
progress.setVisibility(View.GONE);
Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
startActivity(intent);
}
}, 4500);
mLastClickTime = SystemClock.elapsedRealtime();
}
});
But when I clicked on the button several times, it opened the next activity several times
答案1
得分: 2
请移除处理程序并将您的逻辑代码放在那里,例如:
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
// 在此处放置您的逻辑代码
mLastClickTime = SystemClock.elapsedRealtime();
}
});
英文:
Please remove handler and put your logic code there e.g.
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
progress.setVisibility(View.GONE);
Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
startActivity(intent);
}
}, 4500);
mLastClickTime = SystemClock.elapsedRealtime();
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论