package com.hanqi.testapp22; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast; import android.os.Handler; public class lianxiActivity extends AppCompatActivity { TextView tv_2; Handler h =new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { String m = msg.obj.toString(); tv_2.setText(tv_2.getText() + "" + m); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lianxi); tv_2=(TextView)findViewById(R.id.tv_2); } public void bt1_OnClick(View v) { final ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setTitle("拷贝进度"); progressDialog.setMessage("拷贝中..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); final TextView tv_1=(TextView)findViewById(R.id.tv_1); new Thread(new Runnable() { @Override public void run() { for (int i=1;i<=100;i++) { progressDialog.setProgress(i); try { Thread.sleep(200); }catch (Exception e) { e.printStackTrace(); } } runOnUiThread(new Runnable() { @Override public void run() { tv_1.setText("拷贝完成"); } }); progressDialog.dismiss(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(lianxiActivity.this, "拷贝完成", Toast.LENGTH_SHORT).show(); } }); } }).start(); } public void bt2_OnClick(View v) { final ProgressDialog p=new ProgressDialog(this); p.setTitle("拷贝进度"); p.setMessage("拷贝中..."); p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); p.show(); new Thread(){ @Override public void run() { for (int i=1;i<=100;i++) { p.setProgress(i); try { Thread.sleep(200); }catch (Exception e) { e.printStackTrace(); } } p.dismiss(); Message m =Message.obtain(); m.what=1; m.obj="拷贝完成"; h.sendMessage(m); } }.start(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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.hanqi.testapp22.lianxiActivity" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_1" android:onClick="bt1_OnClick" android:text="水平进度对话框和子线程模拟拷贝进度"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_2" android:onClick="bt2_OnClick" android:text="水平进度对话框和子线程模拟拷贝进度"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_2"/> </LinearLayout>