【问题标题】:Updating textView with while loop?用while循环更新textView?
【发布时间】:2013-08-20 11:53:04
【问题描述】:

我编写了一个秒表,但它适用于 java,但不适用于 Android。如果我按下按钮它什么也做不了。会不会是Eclipse安装错了?

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();
                }
            }   
        }

    }

}

感谢您的帮助

【问题讨论】:

  • 你说的是什么意思,但它适用于java,但不适用于Android
  • 我用普通的 java 应用程序尝试了相同的代码,不同之处在于:System.out.println 和 textview.setText
  • 你阻塞了 UI 线程!了解 Android 中的 UI 线程。

标签: java android while-loop textview


【解决方案1】:
public void onClick(View v) {

是一种在单击按钮时应调用的方法,但您必须为您的按钮注册一个 OnClickListener 类。您尝试在 onClick 内部执行此操作。

所以你的类 (MainActivity) 永远不会被注册为 OnClickListener。尝试移动这个:

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

onCreate

【讨论】:

    【解决方案2】:

    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); 
    }
    

    您还在 ui 线程上调用 Thread.sleep(30)。这将阻塞 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 中添加 导致发送此消息的线程 在给定的时间间隔内休眠(以毫秒为单位)。这 不能保证精度 - 线程可能睡眠更多或更少 请求。

    参数 time 睡眠时间,以毫秒为单位。投掷 如果在此线程调用了中断(),则出现 InterruptedException 它正在睡觉另请参阅interrupt()

    【讨论】:

      【解决方案3】:

      您需要做的第一件事:addOnClickListener(),您可以阅读它。我会谷歌它以了解它是如何工作的。

      如果您将此添加到按钮中,请使用

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

      在您的onCreate() 方法中,您已成功将onClickListener() 添加到按钮。
      如果现在按下此按钮,将调用onClick()

      【讨论】:

        猜你喜欢
        • 2017-05-21
        • 1970-01-01
        • 2020-07-17
        • 2012-09-09
        • 2018-08-17
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多