我正在尝试创建一个应用程序,您可以输入一系列按键作为密码。当您按下第一个按钮时,一个方法将跟踪已经过了多长时间,并且在5秒时,按钮按下数据被存储。
long recordingTime = 0;
boolean isRecording = false;
// start timer for total recording time
if(!isRecording) {
isRecording = true;
recordingTime = System.currentTimeMillis();
}
public void update() {
long currentTime = 0;
// get current time
currentTime = System.currentTimeMillis();
// check if timer is over 5 seconds
if(currentTime - recordingTime >= 5000) {
isRecording = false;
Context context = getApplicationContext();
CharSequence text = "Max password time reached.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
答案 0 :(得分:1)
有几种方法可以做到这一点,但这是一种直接的方法来完成它:
view.postDelayed(new Runnable() {
@Override
public void run() {
//store data here
}
}, 5000);