问题
I' using ProgressDialog to show the user that he has to wait and to make the surface of my app "untouchable" while the user has to wait. I added a Button to the progressDialog which should start some actions if certain conditions are true. The problem is that everytime the user press the button the progressDialog gets automatically dismissed. Even if no action is triggered. How can I prevent the progressDialog from getting dismissed when onClick is called?
thx & regards
EDIT:
connectingDialog = new ProgressDialog(this);
connectingDialog.setCancelable(false);
connectingDialog.setCanceledOnTouchOutside(false);
connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
/*if(isPrepared)
startGame();*/
connectingDialog.show(); // does not work, too
}
});
connectingDialog.show();
回答1:
Set the OnClickListener
After the ProgressDialog is Shown.
Unlike some of the other Dialog
s available in Android, ProgressDialog
does not have a setNeutralButton()
method so you need to set the onClickListener
after the ProgressDialog
has been shown, like so:
connectingDialog = new ProgressDialog(this);
connectingDialog.setCancelable(false);
connectingDialog.setCanceledOnTouchOutside(false);
// Create the button but set the listener to a null object.
connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start",
(DialogInterface.OnClickListener) null )
// Show the dialog so we can then get the button from the view.
connectingDialog.show();
// Get the button from the view.
Button dialogButton = connectingDialog.getButton( DialogInterface.BUTTON_NEUTRAL );
// Set the onClickListener here, in the view.
dialogButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick ( View view ) {
if (isPrepared) {
connectingDialog.dismiss();
startGame();
}
// Dialog will not get dismissed unless "isPrepared".
}
});
回答2:
The problem you are having is that ProgressDialog inherits from Dialog/AlertDialog and by default they get dimissed when you press a button on the dialog, regardless of which button it is (positive/negative etc)
The way around this is to intercept the button and override it's listener, but you can only do this after the dialog has been created.
There may be other ways to do this, but I've used this method successfully. Here's an example in code:
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = user.getText().toString();
String password = pass.getText().toString();
if (username != null && username.length() > 0 && password != null && password.length() > 0) {
// store credentials in preferences here
dialog.dismiss()
} else {
Toast.makeText(getActivity(), "Username and/or password cannot be blank.", Toast.LENGTH_SHORT).show();
user.requestFocus();
}
}
});
}
});
As you can see, I use this so that I can validate that my user has actually entered something in some EditText
's before dismissing the dialog, otherwise, prompt them for input.
EDIT:
Worth noting, you need to pass the button a null OnClickListener
initially:
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Enter Credentials")
.setView(v)
.setPositiveButton("OK", null)
.create();
回答3:
Add connectingDialog.setCancelable(false);
was enough for me on Android 4.2.2
来源:https://stackoverflow.com/questions/18579030/prevent-progressdialog-from-getting-dismissed-by-onclick