How to get a progress bar on button click?

2019-09-16 03:49发布

问题:

I am developing an android application in which I have passed a string from one activity to another activity, after getting it from a edit text box by clicking on "OK" button.The string is a url which gives a rss feed.So it takes a little bit time to load it .I want to show a progress bar to keep the interface interactive.How can achieve a progress bar or a progress dialog on that same button click?

My code Snippet for activity is

 EditText Urlis=(EditText)findViewById(R.id.entry);
    final Button button = (Button) findViewById(R.id.ok);
    final Intent i=new Intent(this , RSSReder.class);
final String choice=Urlis.getText().toString();

    i.putExtra("key", choice);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(i);
        }    
    });  
}

}

some part of the code of next activity is

public class RSSReder extends Activity implements OnItemClickListener {

public String RSSFEEDOFCHOICE;
public final String tag = "RSSReader";
private RSSFed feed = null;


/** Called when the activity is first created. */

public void onCreate(Bundle abc) {
    super.onCreate(abc);
    setContentView(R.layout.next1);


    Intent i = getIntent();
    RSSFEEDOFCHOICE =i.getStringExtra("key");

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}

回答1:

You can show the ProgressDialog in the NewActivity if it takes some time in getting rss feeds: take help from the following code:

dialog = ProgressDialog.show(mParent,"","Loading,Please wait...", true);

final Thread t=new Thread(new Runnable() {

            public void run() {

                //get your rss feeds here
            }
        });
        t.start();

        Thread t1=new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                try {
                    t.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                new Handler().post(new Runnable() {
                    public void run() {

                        dialog.cancel();
                                           UpdateDisplay();

                    }
                });

            }
        });
        t1.start();

Hope you can understand what the above code is doing. Customized it a bit and use...:)