我目前正在嘗試使用AlertDialogs。如何「取消」按鈕按?
目前,我在我的AlertDialog中有一個EditText,並且希望將輸入限制爲只有整數。我使用了一個基本的try和catch塊來避免應用程序崩潰,形成NumberFormatException。
但是,我想設置它,以便當用戶嘗試按不正確的輸入按鈕時,輸入不會註冊並且不會取消對話框。
UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
int RawInput = settings.getInt("UpperLimit", 12);
try {
RawInput = Integer.parseInt(input.getText().toString());
}catch (NumberFormatException se) {
Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
//Here I want the app to not register the click and prevent the dialog box from closing.
}
editor.putInt("UpperLimit", RawInput);
editor.apply();
Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
}
});
我可以用什麼方法來達到這個目的?
爲什麼不使用使用XML輸入型的號碼,這樣你不需要檢查工作,任何情況下,一個字符串值 –
我以爲我可以不使用自定義XML而只是刮掉它。但我想不是,最好切換回使用XML。 –
它取決於你。如果你想你可以嘗試Linos答案並保持警告對話框,直到它的數字輸入 –