标签:
package com.hanqi.zuoyee; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class kaobeiActivity extends AppCompatActivity { Button b1,b2,b3; int x=0; Handler h=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { x++; if (x == 100) { h.removeMessages(1); return; } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_kaobei); b1=(Button)findViewById(R.id.bb); b2=(Button)findViewById(R.id.bb2); } public void bt1(View v){ final ProgressDialog pd=new ProgressDialog(this); pd.setTitle("正在拷贝"); pd.setMessage("当前进度"); pd.setCancelable(false); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); new Thread(){ @Override public void run() { for(int i=0;i<101;i++){ try { Thread.sleep(100); }catch (Exception e){ }; pd.setProgress(i); if(i==100){ runOnUiThread(new Runnable() { @Override public void run() { pd.setMessage("已完成"); pd.setMessage(null); Toast.makeText(kaobeiActivity.this, "正在保存", Toast.LENGTH_SHORT).show(); try { Thread.sleep(1000); }catch (Exception e){ }; } }); } if(pd.getProgress()==100){ pd.dismiss(); } } } }.start(); } public void bt2(View v){ final ProgressDialog pd=new ProgressDialog(this); pd.setTitle("正在拷贝"); pd.setMessage("当前进度"); pd.setCancelable(false); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); Message m=Message.obtain(); //m.what=1; h.sendEmptyMessage(1); new Thread(){ @Override public void run() { super.run(); for (int i=0;i<101;i++){ try { Thread.sleep(100); }catch (Exception e){ }; h.sendEmptyMessage(1); runOnUiThread(new Runnable() { @Override public void run() { pd.setProgress(x); if(pd.getProgress()==100){ try { Thread.sleep(1000); }catch (Exception e){ }; pd.dismiss(); } } });} } }.start(); } }
标签:
原文地址:http://www.cnblogs.com/storm47/p/5505787.html