简体   繁体   中英

AlertDialog setOnShowListener never called

I am using a slightly different approach in order to keep the dialog open when a button is pressed:

AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);

final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);

builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialogInterface, int i){
        dialogInterface.cancel();
    }
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
    @Override
    public void onShow(DialogInterface dialogInterface){
        Log.i("TEst", "Doung");
        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    }
}

The dialog is opened a little lower (this works too), but the Log is never called

AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);

final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);

builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.cancel();
    }
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialogInterface) {
        Log.e("TEst", "Doung");
        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    }
});

alertDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM