在显示backPressed
时,我尝试了很多方法来触发progressDialog
()事件。但都没有效果。如果我提供progDialog.setcancelable(true);
,我可以解除progressDialog
,但onBackPressed
()仍然无法触发。
答案 0 :(得分:11)
当ProgressDialog处于活动状态时,如果您按回键执行yopur自己的操作,则必须将setOnCancelListener
设置为progressdialog。
在onCancel()
方法示例中编写你的逻辑你在onBackPressed()事件中编写的整个逻辑就是你必须在这里编写的东西。
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CancelProgressDialog extends Activity {
ProgressDialog myProgressDialog = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a very simple button */
Button b = new Button(this);
this.setContentView(b);
b.setText("Show ProgressBar...");
b.setOnClickListener(myProgressBarShower);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
System.out.println("...any key is pressed....");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
System.out.println("...BackButton is pressed...");
if( (myProgressDialog!= null) && myProgressDialog.isShowing()){
myProgressDialog.dismiss();
}
}
return super.onKeyDown(keyCode, event);
}
/** OnClickListener that fakes some work to be done. */
OnClickListener myProgressBarShower = new OnClickListener() {
// @Override
public void onClick(View arg0) {
// Display an indeterminate Progress-Dialog
myProgressDialog = ProgressDialog.show(CancelProgressDialog.this,
"Please wait...", "Doing Extreme Calculations...", true);
myProgressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
System.out.println("...cancel button is pressed");
// perform your task here
}
});
myProgressDialog.setCancelable(true);
}
};
}
由于 迪帕克