Android (界面编程#5-ProgressDialog)
可以生成一个ProgressDialog对象,设置弹出进度窗口的标题(setTitle),过程提示消息(setMessage),再显示(show)即可,也可直接调用ProgressDialog的showl静态方法,直接显示一个进度框,如果设置ProgressDialog为可以被Cancel的,则能通过手机回退键来中止它,否则只有通过程序内部的逻辑来中止,通过界面及键盘是无法中止这个进度框的。
可以参见如下代码:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class TestProgress extends Activity {
private ProgressDialog progress = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, Menu.FIRST+1, 1, "Open Progress");
menu.add(0, Menu.FIRST+2, 2, "Exit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case Menu.FIRST +1:
{
progress = new ProgressDialog(this);
progress.setTitle("Progress!!");
progress.setMessage("Please wait for the operation...");
progress.setCancelable(true);
progress.show();
//progress = ProgressDialog.show(this, "Progress!", "Please wait for operation...");
break;
}
case Menu.FIRST +2:
{
finish();
break;
}
}
return true;
}
}