要求:要用progressDialog和子线程来模拟显示拷贝进度;进度完成后在主界面提示拷贝完成,分别使用普通方式和消息机制编写。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.MoniwenjianActivity" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="点击拷贝" 17 android:onClick="mn1_OnClick"/> 18 19 <TextView 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="运行结果" 23 android:id="@+id/tt_1"/> 24 25 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.ProgressDialog; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.TextView; 8 9 public class MoniwenjianActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_moniwenjian); 15 } 16 17 public void mn1_OnClick(View v) 18 { 19 final ProgressDialog progressDialog=new ProgressDialog(this); 20 21 progressDialog.setTitle("拷贝进度框"); 22 progressDialog.setMessage("正在拷贝..."); 23 progressDialog.setCancelable(false); 24 25 //设置成水平 26 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 27 28 progressDialog.show(); 29 30 final TextView tt_1=(TextView)findViewById(R.id.tt_1); 31 32 //启动子线程 实现接口 Runnable,run() 33 new Thread(new Runnable() { 34 @Override 35 public void run() { 36 37 //模拟进度变化 38 for (int i=0;i<=100;i++){ 39 progressDialog.setProgress(i); 40 41 try { 42 Thread.sleep(200); 43 }catch (Exception e){ 44 45 } 46 } 47 48 //在子线程里访问UI线程的View 49 runOnUiThread(new Runnable() { 50 @Override 51 public void run() { 52 53 tt_1.setText("拷贝完成"); 54 55 } 56 }); 57 58 //执行完业务代码之后 关闭 59 60 progressDialog.dismiss(); 61 } 62 }).start(); 63 } 64 }