2013-08-20 186 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()