カスタムレイアウト(EditTextのみ)のAlertDialogがあり、[OK]ボタンがクリックされたときにデータを検証したいと思います。検証が失敗した場合、ダイアログを閉じたくありません。
ダイアログのデフォルトボタン(ポジティブとネガティブ)を使用しています。"setPositiveButton(" "、new DialogInterface.OnClickListener()..."を使用すると、ダイアログは常に閉じられます。いくつかの投稿を見て、onClick Listenerをオーバーライドする必要があると言われましたが、機能させることができません。 。これは私が見つけたコードです:
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
ダイアログを表示した後に実行する必要があると表示されているため、このコードをDialogFragment内ではなくアクティビティ内に配置しましたが、mDialogFragment.getDialog()を使用すると、常にnullが返されます。
これは私のダイアログフラグメントの一部です:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.new);
LayoutInflater inflater = getActivity().getLayoutInflater();
dialogView = inflater.inflate(R.layout.edit_license, null);
builder.setView(dialogView)
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
そして、私のアクティビティでは、次のことを行います。
DialogFragment dialog = new MyDialogFragment(true, null);
dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");
AlertDialog alertDialog = (AlertDialog)dialog.getDialog();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setOnClickListener(new CustomListener(alertDialog));
...
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
...
}
}
私が言ったように、(AlertDialog)dialog.getDialog(); 常にnullを返します。何故ですか?検証に問題がある場合、どうすればダイアログを閉じないようにできますか?
ありがとう!