Voici un exemple de code, essayez ceci
AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});
builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});
AlertDialog dialog = builder.create();
dialog.show();
//After calling show method, you need to check your condition and
//enable/ disable buttons of dialog
if(your_condition_true)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button
Pour nagative bouton
dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button
Pour les boutons id : Refrence alert_dialog.xml
Modifié :
Et le setOnShowListener depuis le niveau 8 de l'API (FroYo), fait la même chose,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if(condition)
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
dialog.show();
Édité
new AlertDialog.Builder(this)
.setMessage("This may take a while")
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
// the rest of your stuff
}
})
.show();
Modifier pour la solution complète...
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
builder.setPositiveButton("PositiveButton",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// DO TASK
}
});
builder.setNegativeButton("NegativeButton",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// DO TASK
}
});
// Set EditText to dialog. You can add EditText from xml too.
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
builder.setView(input);
final AlertDialog dialog = builder.create();
dialog.show();
// Initially disable the button
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE)
.setEnabled(false);
// OR you can use here setOnShowListener to disable button at first
// time.
// Now set the textchange listener for edittext
input.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// Check if edittext is empty
if (TextUtils.isEmpty(s)) {
// Disable ok button
((AlertDialog) dialog).getButton(
AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
// Something into edit text. Enable the button.
((AlertDialog) dialog).getButton(
AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});