package com.pingyijinren.helloworld.activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.pingyijinren.helloworld.R; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { progressDialog=new ProgressDialog(this); progressDialog.setIcon(R.mipmap.ic_launcher); progressDialog.setTitle("标题"); progressDialog.setMessage("内容"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "详细信息", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"你点击了详细信息",Toast.LENGTH_SHORT).show(); } }); progressDialog.show(); new Thread(){ @Override public void run() { super.run(); for(int i=0;i<=100;i++){ Message message=new Message(); Bundle bundle=new Bundle(); bundle.putInt("i",i); message.setData(bundle); handler.sendMessage(message); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle bundle=msg.getData(); int i=bundle.getInt("i"); progressDialog.setProgress(i); } }; }