【问题标题】:How can I make my function refresh itself constantly?我怎样才能让我的功能不断刷新自己?
【发布时间】:2014-06-22 17:23:08
【问题描述】:

所以我一直在研究如何使用 while 方法在 Java 中执行循环,但它们对我不起作用。我的时间功能相当简单,但它不会自行更新,因此开始时显示的时间将保持不变。

public class MainActivity extends Activity {

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

        // Time function
        TextView time = (TextView) findViewById(R.id.time);
        time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));

我究竟怎样才能让我的函数重复循环,所以时间显示没有延迟。我一直在尝试很多选择,但似乎没有什么对我有用,我仍在学习 v,v。

谢谢你帮助我:)

编辑 - 我做了什么

// Time function

final boolean keepRunning = true;
Thread thread = new Thread(){
    @Override
    public void run(){

        while(keepRunning){

            TextView time = (TextView) findViewById(R.id.time);
            time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));

            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    TextView time = (TextView) findViewById(R.id.time);
                    time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));;
                }
            });
        }
    }
};

thread.start();

【问题讨论】:

  • 如果提供的答案之一帮助您解决了问题,请接受答案:)
  • 更新了我的答案,您应该可以复制并粘贴它并让它工作。并且取决于您尝试显示的时间有多精确(在正常情况下毫秒有点超过顶部),我真的建议玩弄我放在那里的 Thread.sleep。但是,如有必要,您可以将其省略。
  • @SvenT23 非常感谢,我刚刚通过用 try、catch 函数包围 Sleep 位来修复小错误,现在它工作正常 :) 再次感谢。
  • 是的,很抱歉。我面前没有任何运行环境,所以当我在脑海中写代码时,这样的事情往往会让我忘记 :) 很高兴听到它有帮助,不过
  • 没什么大不了的,它起作用了 :P 再次感谢,我会用这个方法来刷新我的其他活动。

标签: android xml class loops layout


【解决方案1】:

使用线程,在该线程内循环。使用 runOnUiThread 更新您的文本值(不能在工作线程中完成)。

boolean keepRunning = true;
Thread thread = new Thread(){
    @Override
    public void run(){

        while(keepRunning){

            // make the thread wait half a second (if you're only showing time up to seconds, it doesn't need to be updating constantly)
            try{
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    TextView time = (TextView) findViewById(R.id.time);
                    time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));
                }
            });
        }
    }
};

thread.start();

在退出应用之前将 keepRunning 设置为 false 以停止线程。

【讨论】:

  • 我将代码更改为刚刚添加到问题中的代码,它不起作用。我很确定我做错了。
  • 如果除了将textview的值设置为当前时间之外不需要做任何其他事情,请删除while(keepRunning){下面的2行。我会更新我的答案。
  • 好的,所以它不再使应用程序崩溃,而是显示一个空白屏幕。它还告诉我将keeprunning的修饰符更改为final,所以我做到了。
  • 你的 oncreate 中有这个吗?
【解决方案2】:

您可以创建一个处理程序并使用它来触发此事件:

http://developer.android.com/reference/android/os/Handler.html

它有一些方法,比如在未来发起一个事件:

sendMessageAtTime(Message, long) 和 sendMessageDelayed(Message, long)

Handler 本身不能做“重复事件”,但你可以在 Handler 中处理 Message 并通过相同的 sendMessage 方法重新发送新的未来事件。

【讨论】:

    【解决方案3】:

    来自developers website

    new CountDownTimer(30000, 1000) {
    
     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }
    
     public void onFinish() {
         mTextField.setText("done!");
     }
    

    }.start();

    【讨论】:

      猜你喜欢
      • 2013-03-17
      • 1970-01-01
      • 2013-10-09
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 2011-07-18
      相关资源
      最近更新 更多