在if语句中测量时间

时间:2015-03-10 19:53:40

标签: java android button if-statement time

我希望时间开始依赖于第一次点击按钮。在最后一次点击时,它应该给我第一次和最后一次点击之间的时间。在我的代码中,当我想要获取startTime cannot be resolved to a variable时,它会显示difference

这里是代码。

 @Override
    public void onClick(View v) {
        textview1 = (TextView) findViewById(R.id.textView1);
    //Button b = (Button) findViewById(R.id.start_time);
        Random r = new Random();
        RelativeLayout decorView = (RelativeLayout) start_time.getParent();
        int screenWidth = decorView.getWidth();
        int screenHeight = decorView.getHeight();

        i++;

        /*  
        Random r = new Random();

        int x = r.nextInt(R.id.wrap_content);
        int y = r.nextInt(R.id.wrap_content);

        b.setX(x);  
        b.setY(y);
        */ 

        if (i == 1 ) {
            long startTime = SystemClock.elapsedRealtime();
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        if (i == 2 ) {
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        if (i == 3 ) {
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        if (i == 4 ) {
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        if (i == 5 ) {
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        if (i == 6 ) {
            start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
            start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
        }
        else if (i == 7) {
   long difference = SystemClock.elapsedRealtime()  - startTime;

        // here it says startTime cannot be resolved to a variable

        Intent intent1 = new Intent(Game.this, MainScreen.class);
        intent1.putExtra("time",difference);
            finish();
        }

    }

2 个答案:

答案 0 :(得分:1)

更改您的代码
if (i == 1) {
    long startTime = SystemClock.elapsedRealtime();
    ...

long startTime;
if (i == 1) {
    startTime = SystemClock.elapsedRealtime();
    ...

在块中定义变量时(在您的情况下为if块),变量的范围是块,不能在此块之外使用。

编辑:

此外,如果我正确理解您的代码,您需要在第七次点击时获得时差,因此您可以这样做:

else if (i == 7) {
    long difference = SystemClock.elapsedRealtime()  - startTime;

    Intent intent1 = new Intent(Game.this, MainScreen.class);
    intent1.putExtra("time",difference);

    finish();
}

答案 1 :(得分:1)

您在第一个startTime声明中声明了if

 if (i == 1 ) {
    long startTime =    SystemClock.elapsedRealtime();

因此它超出了范围。在onClick()

的开头更快地声明它
public void onClick(View v) {

 textview1 = (TextView) findViewById(R.id.textView1);
//...
//Somewhere here
long startTime;

然后只需在if

中初始化它
 if (i == 1 ) {
    startTime =    SystemClock.elapsedRealtime();