This time, we learned the progressdialog control. For example, in the travel to the West, the Tang Monk was caught by the monsters. The Goku had to save it, but the monsters certainly wouldn't let it go. This was a battle. Of course, the monster cannot beat Wukong. We will use progressdialog to simulate the process of playing monsters, and set it to 100 monsters. After hitting these 100 monsters, we will be able to rescue the masters. figure:
Haha, this time Wukong did not make a shot, and let the eight rings and the sand Monk take a look at the main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <button Android: TEXT = "Wukong rescue Master" Android: Id = "@ + ID/Wukong" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <button Android: text = "ba jie" Android: Id = "@ + ID/Bajie" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <button Android: text = "Sha Seng" Android: id = "@ + ID/shaseng" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> </linearlayout>
Again, several buttons are defined. Next, let's look at the Java source code of activity:
Import android. app. activity; import android. app. progressdialog; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class progressdialogdemo extends activity implements onclicklistener {private progressdialog dialog; private handler mhandler; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. progressdialog); button Wukong = (button) findviewbyid (R. id. wukong); Wukong. setonclicklistener (this); button Bajie = (button) findviewbyid (R. id. bajie); Bajie. setonclicklistener (this); button shaseng = (button) findviewbyid (R. id. shaseng); shaseng. setonclicklistener (this) ;}@ overridepublic void onclick (view v) {// set the handler object, the main task is to process the data sent to the main thread after the new thread is completed. mhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {string name = (string) MSG. OBJ; toast. maketext (progressdialogdemo. this, name + "saving the master", 1 ). show () ;}}; // create progressdialog object dialog = new progressdialog (this); // set the progressdialog style to the progress bar dialog. setprogressstyle (progressdialog. style_horizontal); // sets the maximum value of progressdialog to 100. Here, it is the 100 little monster dialog. setmax (100); // you cannot cancel the progressdialog configuration. You cannot cancel the configuration halfway. Of course, you only need to finish setting the dissdialog. setcancelable (false); string name = NULL; Switch (v. GETID () {case R. id. wukong: // set the name to see who is playing monsters. Name = "Sun Wukong"; dialog. settitle (name); // image dialog. seticon (R. drawable. wukong); // message dialog. setmessage ("Goku playing monsters"); // you can customize doflight (name), break, and case R. id. bajie: // same as above name = ""; dialog. settitle (name); dialog. seticon (R. drawable. bajie); dialog. setmessage ("eight rings playing monsters"); doflight (name); break; case R. id. shaseng: // same as above name = "Sha Monk"; dialog. settitle (name); dialog. seticon (R. drawable. shaseng); dialog. setmessage ("Monk playing monsters"); doflight (name); break ;}} private void doflight (final string name) {// display progressdialogdialog. show (); // new thread () {// Number of monsters completed int COUNT = 0; Public void run () {try {// run the while (count <= 100) {dialog when the number of monsters is less than 100. setprogress (count ++); // sleep for 0.2 seconds. You also need to let them take a break. sleep (200);} dialog. cancel (); // send messages to handler to see who is playing monsters. handler is the message in the main thread = new message (); message. OBJ = Name; mhandler. sendmessage (Message);} catch (interruptedexception e) {dialog. cancel ();}};}. start ();}}
This involves the communication between the subthread and the main thread. With handler, the data run by the subthread can be finally handed over to the main thread. In this chapter, we will talk about the thread. OK, this chapter is complete. Thank you.