這裏是我的第一個問題。 我正在創建一個對話框,以在遊戲菜單中輸入玩家名稱,但想要限制可能的字符(只有字母和數字,如果不可能,至少不包括返回),並防止按下ok按鈕是空的,但我無法弄清楚如何做到這一點。還有一種方法可以將默認操作設置爲恢復正常?我知道很多應用程序都利用它來加快輸入速度。以下是我有:在AlertDialog中需要輸入限制
public String tmPlayerName;
...
public void playerDialog() {
dialogOpen = true;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change player");
alert.setMessage("Enter player name (cannot be empty)");
final EditText input = new EditText(this);
input.setText(tmPlayerName);
input.
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
tmPlayerName = input.getText().toString();
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
編輯:應用程序使用的Android 4.0,並使用谷歌播放服務,如果這是任何人的幫助建造。
看到這個答案:http://stackoverflow.com/a/14192231/2649012 –
鏈接中給出的解決方案限制我輸入任何東西。 關於如何限制空提交的任何想法? –
顯示的答案給出了'想限制可能的字符(只有字母和數字')的解決方案,那麼很容易拒絕空文本if(myEdt.getText()。toString.equals(「」)){}' –