开发者

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

开发者 https://www.devze.com 2023-02-23 23:26 出处:网络
i am beginer to android,i would like to prin开发者_开发技巧t 1 -10 numbers in order like one by one as shown follows

i am beginer to android,i would like to prin开发者_开发技巧t 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?


This does not work, like this, as onClick() is run on the UI thread, blocking it for updates until your onClick() returns. 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.

Here is an example of the usage of AsyncTask. Also have a look at the docs which also illustrates progress updates.


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 and runnable combo OR asynctask.

0

精彩评论

暂无评论...
验证码 换一张
取 消