从未调用AlertDialog setOnShowListener

时间:2017-02-13 22:39:52

标签: java android

我使用稍微不同的方法,以便在按下按钮时保持对话框打开:

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);
    }
}

对话框打开得稍低(这也有效),但永远不会调用日志

1 个答案:

答案 0 :(得分:10)

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();