我目前正在创建 Hangman Android 应用程序,但在注册输入的字母和更新游戏时遇到问题。我使用屏幕键盘在创建的 EditText 中输入一个字母,但没有任何反应。
下面是我正在使用的代码:
// Setting up user input
input = (EditText) findViewById(R.id.inputguess);
input.setFocusable(true);
input.setFocusableInTouchMode(true);
//Getting user input
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String temp;
String newLetter;
newLetter = input.getText().toString();
temp = (String)enteredText.getText();
if (temp.indexOf(newLetter.toUpperCase(Locale.ENGLISH)) >= 0) {
input.setText("");
return true;
}
input.setText(""); // clearing input
entered += newLetter.toUpperCase(Locale.ENGLISH); // adding inputted letter to the entered string
enteredText.setText(temp + newLetter.toUpperCase(Locale.ENGLISH));
word.setText(hideString(text, newLetter.toUpperCase(Locale.ENGLISH)));
我的 EditText 的 XML 代码如下:
<EditText
android:id="@+id/inputguess"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/guessedwords"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="24dp"
android:ems="10"
android:hint="@string/guessletter"
android:inputType="text"
android:maxLength="1"
android:singleLine="true"
android:imeOptions="actionDone">
</EditText>
经过研究,我能想到的唯一原因是所使用的监听器类型,但我并不完全确定。任何帮助将不胜感激(让我知道我是否应该添加更多代码)。
最佳答案
实际上,OnKeyListener 仅适用于硬件键盘。要使用软件(屏幕)键盘,您必须添加 TextWatcher
editText.addTextChangedListener(new TextWatcher() { ... })
关于java - Android Hangman 游戏 - 用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344207/