如下面的代码所示,我有两个计时器。 18秒钟的意思是运行时间更长,但我保留它用于测试目的。我的目的是在用户输入输入后重复4秒定时器,但我希望它重复再次输出loadG1。所以它第一次结束,然后在用户按下进入并重新开始但是首先输出loadG1之后重复。显然这很困难,因为数字(因此loadG1)在计时器的开头就消失了。我怎么能这样做?我需要另一个计时器吗?
final Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
final int[] score = {0};
Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(1000000)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(" "+loadG1);
new CountDownTimer(4000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN){
switch (keyCode){
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer==finalLoadG1){
score[0]++;
}
return true;
default:
break;
}
}
return false;
}
});
}
}.start();
new CountDownTimer(18000, 1000) {
@Override
public void onTick (long millisUntilFinished) {
}
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score[0]);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
}
}.start();
}
});
为了使事情稍微复杂一些,我希望每当4秒计时器运行时暂停更长的(18秒)计时器。因此,只要loadG1可见,我就想要暂停长计时器。当4秒计时器结束并且用户可以输入值(回答)时,我希望更长的计时器继续。现在,这个计时器从头开始。注意:按下按钮时首先输出LoadG1,然后开始4秒。
非常感谢所有支持。
编辑:虽然我的应用程序冻结了,但我尝试过一个我认为可以工作的循环。我被告知我的应用程序没有响应'和模拟器冻结。以下是我在代码中添加的额外部分:int n = 1;
while (n < 2) {
new CountDownTimer(4000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score[0]++;
}
return true;
default:
break;
}
}
return false;
}
});
number.setText(" " + loadG1);
}
}.start();
}
编辑2:不同代码的错误相同... 最后按钮loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
final int[] score = {0};
Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(1000000)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(" "+loadG1);
new CountDownTimer(18000, 1000) {
@Override
public void onTick (long millisUntilFinished) {
}
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score[0]);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
}
}.start();
while (true) {
SystemClock.sleep(4000);
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score[0]++;
}
return true;
default:
break;
}
}
return false;
}
});
}
答案 0 :(得分:0)
你可以拥有类似的东西:
while(true)
{
// 18 minute timer.pause -> not too sure about this one.
// get input
// show output
wait(4 seconds) -> you could use thread.sleep(4000) here
}
编辑: 我的意思是这样的:
while (true) {
// Function that manipulates and accepts values
Function(parameters)
number.setText(" " + loadG1);
thread.sleep(4000);
}