确保 EditText 只返回单个小写字母
我正在将我的旧 Hangman java 游戏移植到 Android 上,以参加一月份的编程期末考试。我已经完成了大部分工作,但我发现我的没有对无效字符进行任何检查。无效字符基本上是除小写字母之外的所有字符。我一直在考虑手动将所有有效字符输入到数组中,并根据该数组检查每个输入。有更简单的方法吗?
以下是从适当的 EditText 捕获输入的代码:
final EditText guessedLetter = (EditText) findViewById(R.id.LetterInput);
final Button enterGuess = (Button) findViewById(R.id.GuessButton);
enterGuess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String guess = guessedLetter.getText().toString(); //Save text to a string
guessedLetter.setText(""); //Clear EditText after input has been saved to a String
editor.putString(GAME_LOGIC_GUESS, guess);
editor.commit();
Log.i(GAME_DEBUG, "Guess: " + guess + " parsed to guess()");
guess();
checkWin();
updateDisplay();
}
}});
再次感谢!
I'm porting my old Hangman java game to android for my programming finals in january. I've gotten most of it working but i have found that my does not do any checks for invalid characters. The invalid characters are basicly everything but lowercase letters. I have been thinking of manually entering all valid characters into an array and check each input against that. Is there an easier way to do it?
Here is the code that catches the input from the appropriate EditText:
final EditText guessedLetter = (EditText) findViewById(R.id.LetterInput);
final Button enterGuess = (Button) findViewById(R.id.GuessButton);
enterGuess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String guess = guessedLetter.getText().toString(); //Save text to a string
guessedLetter.setText(""); //Clear EditText after input has been saved to a String
editor.putString(GAME_LOGIC_GUESS, guess);
editor.commit();
Log.i(GAME_DEBUG, "Guess: " + guess + " parsed to guess()");
guess();
checkWin();
updateDisplay();
}
}});
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 字符怎么样。 isLowerCase(char ch),或者 Character.getType(ch) == LOWERCASE_LETTER
What about using Character.isLowerCase(char ch), or alternatively Character.getType(ch) == LOWERCASE_LETTER
您需要首先使用 TextView 的 setKeyListener 方法设置 KeyListener。然后,您将重写您设置的 KeyListener 的 onKeyDown 方法并侦听输入。如果输入的不是字符(即:A - Z),那么您将丢弃输入并取消事件。如果输入的是字符,则应先将字符转换为小写,然后再将其添加到文本框。
编辑-
刚刚注意到您没有在按键时执行此操作,而是用户必须点击“确定”。这让您的生活更轻松。
当用户按下“确定”时,检查文本是否为有效字符(即:只有一个字符且位于 A - Z 之间)。如果不是,请对用户大喊大叫。如果是,请检查它是否为小写或自行将其转换为小写。
http://developer.android.com /reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html#toLowerCase(char< /a>)
You would want to first set a KeyListener using the setKeyListener method of the TextView. You would then override the onKeyDown method of the KeyListener you set and listen for input. If what is typed is NOT a character (ie: A - Z) then you would throw out the input and cancel the event. If what was typed was a character you should convert the character to lowercase before adding it to the text box.
Edit -
Just noticed you're not doing this on key presses but instead the user has to hit "Ok". This makes your life easier.
When the user presses "Ok" check to see if the text is a valid character (ie: Only one character and is between A - Z). If it's not, yell at the user. If it is, either check to see if it's lowercase or conver it to lowercase yourself.
http://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html#toLowerCase(char)
我发现对于这个特定问题,最简单的解决方案是一个仅填充有效字符的数组。因此,我没有将无效字符和符号列入黑名单,而是将相对较少的有效字符和符号列入白名单。
如果将来有人需要的话,这里是数组:
使用循环进行比较:
I found that for this particular problem the easiest solution would be an array filled with only the valid characters. So instead of blacklisting non-valid characters and symbols i simply whitelist the relatively few that are valid.
Here is the array if anyone needs it in the future:
Using a loop for comparing: