【问题标题】:how to make number running from 60 to 0 and print on textview in android studio如何使数字从 60 运行到 0 并在 android studio 的 textview 上打印
【发布时间】:2015-11-22 15:15:51
【问题描述】:
       Runnable r = new Runnable(){
       public void run(){
            try{
                    For (int i = 60; i >= 1; i--) {        
                    System.out.print(i + "\r");
                    // Let the thread sleep for a while.
                    Thread.sleep(1000);
                    TextView box2 = (TextView) findViewById(R.id.box2);
                    box2.setText("\r" + i + " sec left");
                }
            } catch (InterruptedException e) {
            }
        }

如何使数字从 60 运行到 0 并在 android studio 的文本视图上打印,以及在 android studio 的文本视图中打印的正确方法是什么。谢谢您的帮助!

【问题讨论】:

  • 使用 handler.postDelayed();带有可运行文件的方法。
  • 非常感谢兄弟的帮助!
  • 我还添加了一个答案来解释更多。

标签: java android printing numbers countdown


【解决方案1】:

首先在 onCreate() 中初始化你的 TextView:

TextView box2 = (TextView) findViewById(R.id.box2);

然后您可以使用带有 postDelayed() 方法的处理程序来创建倒计时:

final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
    public void run() {
        for (int i = 60; i >= 1; i--) {
            box2.setText("\r" + i + " sec left");
        }
        handler.postDelayed(this, 1000);
    }
}, 0);

【讨论】:

  • 它有效!非常感谢。但是为什么 android studio 无法读取回车符(\r)?
  • \r 没有任何意义。我只知道 \n 表示换行。
  • 我总是在eclipse中使用\r来打印“仅一行”中的数字序列,但是android studio无法读取它。
  • 嗯,只要不写\n,还是会写成一行。所以不用写\r。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多