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 Shifu" android: id = "@ + id/bajie"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "Sha Seng rescue Master" 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;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
// TODO Auto-generated method stub
Super. 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 );
}
@ Override
Public void onClick (View v)
{
// Set the Handler object, which is mainly used to process the data that is handed over to the main thread after the new thread is started.
Mhandler = new Handler (){
@ Override
Public void handleMessage (Message msg)
{
String name = (String) msg. obj;
Toast. makeText (ProgressDialogDemo. this, name + "", 1). show ();
}
};
// Create a ProgressDialog object
Dialog = new ProgressDialog (this );
// Set the ProgressDialog style as a progress bar
Dialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL );
// Set the maximum value of ProgressDialog to 100, which is 100 little monsters.
Dialog. setMax (100 );
// You cannot cancel the ProgressDialog setting. You cannot cancel the setting halfway. Of course, you have to finish the setting at 100.
Dialog. 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 ("Wukong playing monsters ");
// Customize the Fighting Method
DoFlight (name );
Break;
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 ("sashan playing monsters ");
DoFlight (name );
Break;
}
}
Private void doFlight (final String name)
{
// Display ProgressDialog
Dialog. show ();
// Create a new thread
New Thread ()
{
// Number of monsters completed
Int count = 0;
Public void run ()
{
Try
{
// How to run when the number of monsters is less than 100
While (count <= 100 ){
Dialog. setProgress (count ++ );
// Sleep for 0.2 seconds. You have to take a rest for them.
Thread. sleep (200 );
}
Dialog. cancel ();
// Send a message to handler to see who is playing monsters. handler is in the main thread.
Message message = 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.
From: kangkangz4 Column