2015-12-09 38 views
0

我不知道什麼是錯的。 TextView不會改變它保持打印00. 我希望得到一個簡單的計時器,如0,1,2,.... 我想使用線程而不是像定時器或時間任務類。 謝謝你的時間。 下面的代碼:Android Studio中的計時器

public class MainActivity extends AppCompatActivity { 

TextView time; 
Button start,reset,exit; 
Thread aspetta; 
int i; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    time = (TextView)findViewById(R.id.tempo); 
    start = (Button)findViewById(R.id.inizio); 
    reset = (Button)findViewById(R.id.reset); 
    exit = (Button)findViewById(R.id.exit); 
    aspetta = new Thread(); 
    i = 0; 

    start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      waitAMinute(aspetta,time,i); 
     } 
    }); 
} 
public void waitAMinute(Thread t,TextView tv,int x) { 
    x = 0; 
    while (true) { 
     try { 
      t.sleep(1000); 
      tv.setText(Integer.toString(x)); 
      x++; 
     } catch(InterruptedException e) { 
      tv.setText("Error!"); 
     } 
    } 
} 

}

+0

我省略了之類的東西super.onCreate ......但這些都是我的源代碼。 – Giovanni

+3

你也省略了這個事實,即Android Studio是IDE,並且完全不相關你用什麼編輯器編寫你的** **代碼** android ** –

+0

你創建了一個線程,但是你的代碼實際上並沒有運行線。在我的手機上這麼短的答案對不起:-) – Blundell

回答

0

你真的不需要創建任何線程來實現自己的目標。使用Java Timer()對象設置對代碼的重複回調相對簡單,您可以在其中更新文本框。

準確地說,你在這裏所做的並不是100%清楚,我假設你點擊inizio時,你希望文本框中的文本開始遞增,如1,2,3等。當你點擊重置按鈕時,你希望它從1重新開始。 (注意:爲了使這個行爲像一個普通的秒錶,你可以在定時器啓動後將inizio按鈕的文本改爲單詞「stop」等)。

這段代碼只是爲了向你展示使用定時器設置重複間隔時間,在那裏你可以更新你的UI顯示變量的狀態:

package com.example.myapp3; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.Timer; 
import java.util.TimerTask; 

public class MyActivity extends Activity { 

    TextView time; 
    Button start, reset, exit; 
    int i; 

    Timer timer = null; 
    int tickCount; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     time = (TextView) findViewById(R.id.tempo); 
     start = (Button) findViewById(R.id.inizio); 
     reset = (Button) findViewById(R.id.reset); 
     exit = (Button) findViewById(R.id.exit); 

     i = 0; 

     reset.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       scheduleTimer(); 
       tickCount = 0; 
       time.setText(""); 
      } 
     }); 

     start.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       scheduleTimer(); 
      } 
     }); 
    } 


    public void scheduleTimer() { 
     if (timer != null) { 
      timer.cancel(); 
     } 

     timer = new Timer(); 

     TimerTask myTask = new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         final TextView myTv = time; 
         myTv.setText(Integer.toString(++tickCount)); 
        } 
       }); 
      } 
     }; 

     timer.schedule(myTask, 1000, 1000); 
    } 
}