简体   繁体   English

如何在Android的文本视图中打印1到10个数字?

[英]how to print 1 to 10 numbers at text view in android?

i am beginer to android,i would like to print 1 -10 numbers in order like one by one as shown follows 我是android的初学者,我想按如下所示依次打印1 -10个数字

1 2 3 4 5 6 7 8 9 10 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 我想打印那些数字,如1 2 3秒后3 3秒4后3秒......等等

i have written code as follows to print 1-10 but it displaying only 10 . 我已经写出如下代码来打印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);
        }

please is there any solutions for that? 请问有什么解决办法吗?

This does not work, like this, as onClick() is run on the UI thread, blocking it for updates until your onClick() returns. 这样不起作用,因为onClick()在UI线程上运行,阻止它进行更新,直到onClick()返回。 While

You can solve that with an AsyncTask , where the doInBackground() method is doing the loop, sending the number to print via publishProgress() and have onProgressUpdate() update the TextView. 您可以使用AsyncTask解决此问题,其中doInBackground()方法正在执行循环,通过publishProgress()发送要打印的数字,并让onProgressUpdate()更新TextView。

Here is an example of the usage of AsyncTask . 这是AsyncTask用法的示例 Also have a look at the docs which also illustrates progress updates. 还可以查看还说明进度更新的文档

There is a function is textview called append . 有一个名为textview的函数,称为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 and runnable combo OR asynctask . 要在一定间隔后打印它们,可以使用Handlerrunnable combo或asynctask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM