2017-08-13 49 views
1

我正在学习Java和Android Studio。Andriod Studio Java

我有下面的代码,我希望在屏幕上可以计算多达300万。

它编译并运行,最后在模拟器上显示300万。我的问题是如何在循环中强制重绘/显示文本框?

/** Called when the activity has become visible. */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(msg, "The onResume() event"); 
     TextView textbox1=(TextView)findViewById(R.id.TextView1); 
     for(double l=0; l<=3000000; l++){ 
      textbox1.setText("" + l); 
     } 
    } 
+0

你能解释一下吗?你需要做什么? –

+0

描述你所期待的输出 –

+0

我希望看到在文本框中显着增加的值。 – Iain

回答

0

onResume运行完毕后的视图只禁用

您可能希望在textview中设置文本,更新活动中的某个状态(如int字段),并注册一些代码以在一段时间后运行并增量。请看使用Handler,AsyncTask或其他选项推迟代码。

这里有一个快速而肮脏的例子Handler

final long DELAY_MILLIS = 50; 
final Handler handler = new Handler(); 
int num = 0; 
final Runnable runnable = new Runnable() { 
    public void run() { 
    if (num >= 3000000) return; 
    textbox1.setText("" + num); 
    num++; 
    // re-register ourself to run in DELAY_MILLIS; 
    handler.postDelayed(runnable, DELAY_MILLIS); 
    } 
}; 

TextView textbox1; 

protected void onResume() { 
    // more efficient to look this up once 
    this.textbox1 = (TextView)findViewById(R.id.TextView1); 
    runnable.run(); // will register itself to re-run 
}