1)效果图如下

2)关键代码如下
package com.example.androidtestapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.View;
import android.app.ProgressDialog;
public class MainActivity extends Activity {
private Button button1=null;
private ProgressDialog progressDialog1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View view){
progressDialog1=ProgressDialog.show(MainActivity.this, "loading...", "please wait...",true);
//为看到明显的效果,创建线程以暂停3s.
new Thread(){
public void run(){
try{sleep(3000);}
catch(Exception e){e.printStackTrace();}
finally{progressDialog1.dismiss();}
}
}.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
本文介绍了一个简单的Android应用程序示例,展示了如何在点击按钮后显示ProgressDialog,并通过线程模拟长时间运行的任务后关闭对话框。此示例适用于希望了解如何在Android应用中使用ProgressDialog进行用户交互的开发者。
211

被折叠的 条评论
为什么被折叠?



