2017-04-19 43 views
-1

我试图打印在textwiew中更改的消息。 当我这样做的问题是,该应用程序正在等待循环的结尾来放置结果。Android应用程序中的动态文本视图

public class Testloop extends AppCompatActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_testloop); 
     String message = "test : "; 
     for(int x = 1; x < 20; x = x + 1) { 
      message +=x; 
      int timeInMills = 1000; // time in ms 
      SystemClock.sleep(timeInMills); 
      TextView textView = (TextView) findViewById(R.id.txte); 
      textView.setText(message); 
     } 

任何帮助将是伟大的!

回答

0

onCreate方法不是一个好的地方。循环将在活动显示前完成,并且您设置的最后一个字符串将是唯一显示的字符串。尝试使用带有处理程序的Runnable,如下所示:

private Handler handler = new Handler(); 
handler.postDelayed(runnable, 100); 

这会告诉runnable在100ms内运行。然后创建可以运行这样的:

private Runnable runnable = new Runnable() { 
    @Override 
    public void run() { 
     //Set your text here 
     textView.setText(message); 
     // here you set the runnable to run again in 100ms 
     handler.postDelayed(this, 100); 
    } 
}; 

把你的消息字符串数组和索引通过它每次可运行的运行。

相关问题