2013-08-20 188 views
0

我編寫了一個秒錶,但它適用於java,但不適用於Android。如果我按下按鈕,它什麼也不做。難道是,也許Eclipse安裝錯誤?用while循環更新textView?

package com.example.stoppuhr; 

import java.text.SimpleDateFormat; 

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


public class MainActivity extends Activity implements OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    long start; 
    long millis; 

    public void onClick(View v) { 
     Button start1 = (Button)findViewById(R.id.button1); 
     Button button2 = (Button)findViewById(R.id.button2); 
     TextView textView1 = (TextView)findViewById(R.id.textView1); 
     start1.setOnClickListener(this); 

我想這裏是問題所在。

 if (v.getId() == start1.getId()){ 
      textView1.setText("moldu"); 
      start = System.currentTimeMillis(); 

      while (true){ 
       millis = System.currentTimeMillis()-start; 



       SimpleDateFormat sdfDate = new SimpleDateFormat("mm:ss.SSS");//dd/MM/yyyy 
       String ausgeben = sdfDate.format(millis); 

       textView1.setText(ausgeben); 
       try { 
        Thread.sleep(30); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

} 

謝謝您的幫助

+0

你是什麼意思,但它適用於java,但不適用於Android_? – mabbas

+0

我嘗試了與普通Java應用程序相同的代碼,區別在於:System.out.println和textview.setText – hobbyprogrammer

+1

您正在阻止UI線程!閱讀Android中的UI線程。 – Mike

回答

3
public void onClick(View v) { 

是一個一個按鈕被點擊時應該調用的方法,但你必須註冊爲您的按鈕類添加一個OnClickListener。您嘗試在onClick內執行此操作。

所以你的班級(MainActivity)永遠不會註冊爲OnClickListener。嘗試移動此:

Button start1 = (Button)findViewById(R.id.button1); 
start1.setOnClickListener(this); 

onCreate

3

onCreate

Button start1,button2 
TextView textView1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    start1 = (Button)findViewById(R.id.button1); 
    button2 = (Button)findViewById(R.id.button2); 
    textView1 = (TextView)findViewById(R.id.textView1); 
    start1.setOnClickListener(this); 
} 

您還呼籲Thread.sleep(30)在UI線程上初始化您的看法。這將阻止ui線程。

http://developer.android.com/training/articles/perf-anr.html

從文檔

如果實現ThreadHandlerThread,請確保您的UI線程不會阻塞在等待工作線程的進修不叫Thread.wait()Thread.sleep()報價。

確保您不要在UI線程上調用Thread.sleep()

我建議你看看倒數計時器。

  1. Android Thread for a timer

  2. Countdowntimer in minutes and seconds

public static void sleep (long time)毫秒

在API級別1 //導致其發送該消息到 睡眠對於給定的線程時間間隔(以毫秒爲單位)。不保證精確度 - 線程可能會睡眠多於或少於 請求。

參數時間以毫秒爲單位的休眠時間。拋出 InterruptedException的,如果中斷()被調用該線程同時 它睡覺另請參見中斷()

+0

也Thread.sleep需要時間以毫秒爲單位 – Blackbelt

+1

@blackbelt謝謝你,我錯過了那部分。 – Raghunandan

0

你需要做的第一件事:addOnClickListener(),你可以閱讀它。我會谷歌瞭解它是如何工作的。

如果您將此添加到您的按鈕,通過在onCreate()方法使用

Button buttonname = (Button) findViewById(R.id.yourbuttonid); 
buttonname.setOnClickListener(this); 

,你成功添加了onClickListener()到按鈕。
如果現在按下此按鈕,將調用onClick()