【发布时间】:2014-11-01 15:00:23
【问题描述】:
我正在构建一个带有紧急警报按钮的应用程序。我想在按住按钮 3 秒钟后显示一条 toast 消息,所以如果有人误按它,它不会发送,但我不知道该怎么做。
public long startTime = 0;
Button btn = (Button)findViewById(R.id.btnAlert);
btn.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
if((System.currentTimeMillis()- startTime)> 3000){
Toast.makeText(getApplicationContext(), "Alert Received! Emergency Services Will Arrive At Your Location Shortly", Toast.LENGTH_LONG).show();
}
return true;
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
startTime = System.currentTimeMillis();
return true;
}
return false;
}
});
【问题讨论】:
-
我尝试在 toast 消息上设置一个计时器,以便在 3 秒后显示它,但我希望将计时器放在按钮上
标签: android eclipse button time onclick