如何在android中的文本视图中打印1到10个数字?
我是 android 初学者,我想按顺序打印 1 -10 个数字,如下所示
1 2 3 4 5 6 7 8 9 10
我想打印这些数字,例如 1 3secs after 2、3secs after 3,3secs after 4......所以
我编写了如下代码来打印 1-10 但它只显示 10 。
@Override
public void onClick(View v) {
name=((TextView)findViewById(R.id.textView1));
for(int i=0;i<11;i++){
name.setText("hai"+i);
}
请问有什么解决办法吗?
i am beginer to android,i would like to print 1 -10 numbers in order like one by one as shown follows
1
2
3
4
5
6
7
8
9
10
i would like to print those numbers like 1 3secs after 2, 3secs after 3,3secs after 4......so on
i have written code as follows to print 1-10 but it displaying only 10 .
@Override
public void onClick(View v) {
name=((TextView)findViewById(R.id.textView1));
for(int i=0;i<11;i++){
name.setText("hai"+i);
}
please is there any solutions for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不起作用,就像这样,因为
onClick()
是在 UI 线程上运行的,会阻止它进行更新,直到onClick()
返回。虽然您可以使用
AsyncTask
来解决这个问题,其中doInBackground()
方法正在执行循环,通过
publishProgress()
发送要打印的数字,并让onProgressUpdate()
更新 TextView。这是示例
AsyncTask
的用法。另请参阅 docs,其中也说明了进度更新。This does not work, like this, as
onClick()
is run on the UI thread, blocking it for updates until youronClick()
returns. WhileYou can solve that with an
AsyncTask
, where thedoInBackground()
method is doing the loop,sending the number to print via
publishProgress()
and haveonProgressUpdate()
update the TextView.Here is an example of the usage of
AsyncTask
. Also have a look at the docs which also illustrates progress updates.textview 有一个函数,名为 append< /a>.您可以使用此函数来达到以下目的:
关于如何使用此函数:
name.append("\n" + i);
要在一段时间间隔后打印它们,您可以
Handler
和runnable
组合或asynctask
。There is a function is textview called append. You can this function for the purpose:
On how to use this function:
name.append("\n" + i);
For printing them after an interval, you can
Handler
andrunnable
combo ORasynctask
.