开发者

how to change button text dynamically for every 3 sec in android? [closed]

开发者 https://www.devze.com 2022-12-20 02:35 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its curre开发者_StackOverflownt
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its curre开发者_StackOverflownt form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

in my application i want to change button text for every 3 sec.


Set up a Timer to run a TimerTask every 3 seconds. The TimerTask only has to call the button's setText method to change the button's text. You would have to do this within the UI thread, so you should use post to run a Runnable object that will perform the update an the correct thread.

For example, in the following activity, the letter "A" is added to the button's text every three seconds:

public class ButtonTest extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button subject = new Button(this);
        subject.setLayoutParams((new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT)));
        subject.setText("A");
        setContentView(subject);

        Timer timing = new Timer();
        timing.schedule(new Updater(subject), 3000, 3000);
    }

    private static class Updater extends TimerTask {
        private final Button subject;

        public Updater(Button subject) {
            this.subject = subject;
        }

        @Override
        public void run() {
            subject.post(new Runnable() {

                public void run() {
                    subject.setText(subject.getText() + "A");
                }
            });
        }
    }
}


Have a look at this neat little example: http://developer.android.com/resources/articles/timed-ui-updates.html

All you need is a handler and a task! Within the Task do your actions (setting the text) and set up the next handler event using Handler.postDelayed.

But keep the following in mind: I would strongly advise you to set up the first timer event in the activity's onResume() method and NOT in its onCreate method. Also, remove (cancel) your handler in onPause()!

To understand the meaning of this, have a look at an activity's lifecycle.

That way you prevent to have anything happening while your activity is in the background and not visible!


@deepthi: here is an detail example to to display text in a button at specific time interval

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageView;

public class ButtonActivity extends Activity {

private static final long GET_DATA_INTERVAL = 2000;
int images[] = {R.drawable.kanya,R.drawable.kumba};
private String[] textfirst={"nsc","bsc","nasadq","tcs","mds","mac","manipal"};

int index = 0;
ImageView img;
Button btn;
Handler hand = new Handler();
Handler hand1 = new Handler();
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    img = (ImageView) findViewById(R.id.image);
    btn = (Button) findViewById(R.id.button1);
    hand1.postDelayed(run1, GET_DATA_INTERVAL);      
}

Runnable run1 = new Runnable() {
    @Override
    public void run() {
        btn.setText(textfirst[index++]);
        if (index == textfirst.length)
            index = 0;
        hand1.postDelayed(run1, GET_DATA_INTERVAL);
    }
};


Using AsyncTask you can do something like this to update a Button named button1

private class UpdateButton extends AsyncTask<Void, String, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        int i = 0;
        while ( true ) {
            publishProgress("" + i++);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                return (Void)null;
            }
        }
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        button1.setText(values[0]);
    }       
}

to start button1 update

(new UpdateButton()).execute((Void)null);
0

精彩评论

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

关注公众号