Android Studio Java循环中的更新文本

第一次在这里学习编码语言..我在Java的许多部分都在苦苦挣扎,但我正在慢慢学习。

我正在制作应用程序并且必须显示动态更新文本。 所以下面的代码是我的尝试,它运作得很好(有点)。 问题是当程序运行for循环时它不会更新屏幕。

因此,当我按下主活动中的按钮时,它会运行一个名为play的活动。 它有14个文本字段为0,并且应该自动将它们逐个更新为7。

但是在运行下面的代码后,它只是在片刻后显示了14个7。 (也没有0开始!)任何用于显示屏幕的代码,以便我可以将它放在setText()下面?

如果之前有人问这个问题,我很抱歉,但我真的找不到任何在线答案。

public class Play extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_play); Initialization(); } public void Initialization() { TextView[] textViews = new TextView[] { (TextView)findViewById(R.id.player1_1), (TextView)findViewById(R.id.player1_2), (TextView)findViewById(R.id.player1_3), (TextView)findViewById(R.id.player1_4), (TextView)findViewById(R.id.player1_5), (TextView)findViewById(R.id.player1_6), (TextView)findViewById(R.id.player1_7), (TextView)findViewById(R.id.player2_1), (TextView)findViewById(R.id.player2_2), (TextView)findViewById(R.id.player2_3), (TextView)findViewById(R.id.player2_4), (TextView)findViewById(R.id.player2_5), (TextView)findViewById(R.id.player2_6), (TextView)findViewById(R.id.player2_7) }; for (int i = 0; i < 8; i++) { for (int j = 0; j < 14; j++) { textViews[j].setText(Integer.toString(i)); try { Thread.sleep(25); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } } 

}

目前,您在UI线程上调用Thread.sleep(25) 。 这意味着UI线程将被阻止,不会更新您的文本视图。 在所有阻塞之后,它在for循环之后最终被解除阻塞,因此您可以立即看到所有文本字段更新。

为了获得更好的用户体验,请不要告诉UI线程hibernate。

你需要一些能在一段时间后完成一些工作的东西。 android.os.Handler是一个不错的选择。 它有一个postDelayed方法,可以在一段时间后执行您希望它执行的操作。

IMO,直接调用postDelayed确实可以完成你的工作。 但每个postDelayed调用包含另一个,以便在几秒钟后更新下一个文本视图。 这意味着你将最终得到14层嵌套! 这不是很干净的代码,是吗?

那么,让我介绍一下Timer类!

这里是:

 import android.os.Handler; public class Timer { private Handler handler; private boolean paused; private int interval; private Runnable task = new Runnable () { @Override public void run() { if (!paused) { runnable.run (); Timer.this.handler.postDelayed (this, interval); } } }; private Runnable runnable; public int getInterval() { return interval; } public void setInterval(int interval) { this.interval = interval; } public void startTimer () { paused = false; handler.postDelayed (task, interval); } public void stopTimer () { paused = true; } public Timer (Runnable runnable, int interval, boolean started) { handler = new Handler (); this.runnable = runnable; this.interval = interval; if (started) startTimer (); } } 

这件事很酷。

为了满足这些要求,我们将一个countertextViewToUpdate添加到您的活动类中,以便我们知道它的编号和下一个要更新的文本视图。 哦,并将textViews变量移动到类级别:

 int counter = 1; int textViewToUpdate = 0; TextView[] textViews; 

并在onCreate初始化onCreate

 textViews = new TextView[] { (TextView)findViewById(R.id.player1_1), (TextView)findViewById(R.id.player1_2), (TextView)findViewById(R.id.player1_3), (TextView)findViewById(R.id.player1_4), (TextView)findViewById(R.id.player1_5), (TextView)findViewById(R.id.player1_6), (TextView)findViewById(R.id.player1_7), (TextView)findViewById(R.id.player2_1), (TextView)findViewById(R.id.player2_2), (TextView)findViewById(R.id.player2_3), (TextView)findViewById(R.id.player2_4), (TextView)findViewById(R.id.player2_5), (TextView)findViewById(R.id.player2_6), (TextView)findViewById(R.id.player2_7) }; 

首先在课堂上创建一个计时器:

 Timer t; 

Initialization方法中,创建一个runnable。 这个runnable将每隔几秒运行一次。

 Runnable r = new Runnable () { @Override public void run() { PlayActivity self = PlayActivity.this; self.textViews[self.textViewToUpdate].setText(Integer.toString(self.counter)); self.textViewToUpdate++; if (self.textViewToUpdate == self.textViews.length) { self.textViewToUpdate = 0; self.counter++; if (self.counter == 8) { self.t.stopTimer(); } } } } 

然后,我们创建计时器并运行它:

 t = new Timer(r, interval, true); 

您可以用您喜欢的任何数字替换interval ,也许250 (0.25秒)?

和砰! 你做到了!

所有这一切都发生得太快,你无法看到。 正如您可以轻松计算的那样, 7x25ms = 175ms ,其传递速度非常快,您甚至可能没有注意到屏幕上的数字会发生变化。

我建议你把延迟增加到500毫秒或那个范围内,这样你实际上可能会看到差异或完全改变逻辑(你可以使用CountdownTimer,Tick时间为500毫秒并更改onTick的文本)方法,谷歌吧)。

如果它仅用于测试目的,为什么不添加按钮然后在该按钮的单击上调用Initialization ? 这样你就可以从0开始观察值的变化。