效果图:
原始界面 点击按钮运行 运行后界面
实现代码:
public class MainActivity extends AppCompatActivity {
private Button bt;
private TextView tv;
public ProgressDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.button);
tv=(TextView)findViewById(R.id.text);
bt.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
final CharSequence strDialogTitle=getString(R.string.app_about);
final CharSequence strDialogBody=getString(R.string.app_msg);
//显示对话框
myDialog=ProgressDialog.show(MainActivity.this,strDialogTitle,strDialogBody,true);
tv.setText(R.string.result);
//创建一个空线程模拟运行
new Thread(){
public void run(){
try{
//设置线程睡眠3秒
sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
finally {
//删除所创建的myDialog对象
myDialog.dismiss();
}
}
}.start();//开始运行线程
}
}
}