当前位置: 代码迷 >> Android >> 从未调用AlertDialog setOnShowListener
  详细解决方案

从未调用AlertDialog setOnShowListener

热度:19   发布时间:2023-08-04 10:54:06.0

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

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

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

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();
  相关解决方案