Packagecom.example.wang.myapplication;ImportAndroid.app.ProgressDialog;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classZuoyeActivity4extendsappcompatactivity {Button bt_1; Button bt_2; TextView tv_1; Handler h=NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); if(msg.what==1) {Tv_1.settext ("Copy Complete"); } Else if(msg.what==2) {Tv_1.settext (""); } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_ZUOYE4); Bt_1=(Button) Findviewbyid (r.id.bt_1); Bt_1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {FinalProgressDialog progressdialog=NewProgressDialog (ZuoyeActivity4. This); Progressdialog.settitle ("Copy Progress"); Progressdialog.setmessage ("Copy ..."); Progressdialog.setprogressstyle (progressdialog.style_horizontal); Progressdialog.show (); NewThread () {@Override Public voidrun () { for(inti=0;i<=100;i++) {progressdialog.setprogress (i); Try{Thread.Sleep (100); } Catch(Exception e) {e.printstacktrace (); }} Progressdialog.dismiss (); Runonuithread (NewRunnable () {@Override Public voidrun () {Toast.maketext (ZuoyeActivity4. This, "Copy Complete", Toast.length_short). Show (); } }); }}.start (); } }); Tv_1=(TextView) Findviewbyid (r.id.tv_1); Bt_2=(Button) Findviewbyid (r.id.bt_2); Bt_2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//general methods to access components across threads//Runonuithread (New Runnable () {//@Override//Public void Run () {//Tv_1.settext ("");// }// });H.sendemptymessage (2); FinalProgressDialog progressdialog=NewProgressDialog (ZuoyeActivity4. This); Progressdialog.settitle ("Copy Progress"); Progressdialog.setmessage ("Please, later ...."); Progressdialog.setprogressstyle (progressdialog.style_horizontal); Progressdialog.show (); NewThread (NewRunnable () {@Override Public voidrun () { for(inti=0;i<100;i++) {progressdialog.setprogress (i); Try{Thread.Sleep (100);} Catch(Exception e) {e.printstacktrace (); }} Progressdialog.dismiss (); H.sendemptymessage (1); }}). Start (); } }); }}
Java
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.wang.myapplication.ZuoyeActivity4"android:orientation= "vertical"> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start Copy file-Normal mode"Android:id= "@+id/bt_1"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start copying files--message mechanism"Android:id= "@+id/bt_2"/> <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#ccc"android:gravity= "Center"android:textsize= "16SP"Android:id= "@+id/tv_1"/></LinearLayout>
XML
1. Normal Mode display results
2-Message mechanism results
ProgressDialog and sub-threading simulations show copy Progress