入力が有効でない場合に、入力を検証し、アラート ダイアログを画面に表示し続ける方法を次に示します。実際には、ダイアログを削除して、その新しいコピーを作成します。
setPostiveButton の onClick 関数で、検証を行います。あるべき姿でない場合は、ユーザーにトーストを表示します。次に、ダイアログで removeDialog を呼び出します。次に、これはトリッキーな部分です。ダイアログで非同期に showDialog を呼び出します(該当する場合は引数を使用)。また、ユーザーの入力を失わないように、ユーザーが入力した値を、ダイアログを呼び出すバンドルに入れる必要があります。もちろん、ダイアログのセットアップ コードは、バンドル内のこれらの値を探して、ダイアログ フィールドに適切に事前設定する必要があります。
したがって、コードは次のようになります。
alert.setPositiveButton(id,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
if ((your validation conditions)) {
// validation conditions not satisfied
removeDialog(myDialogId);
Toast.makeText(blah blah).show();
// now create a new args bundle for the dialog
Bundle newArgs = new Bundle();
// now copy whatever you need from the args used to invoke to dialog
newArgs.putIntegerArrayList("items", myList);
// now save the user's input in a bundle
newArgs.putString("dialogToFieldContent", toString);
final Bundle finalArgs = newArgs;
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
showDialog(myDialogId, finalArgs);
}
});
}
else {
// if everything is ok
}
}
});