Java Android Hangman游戏-用户输入
我目前正在创建一个Hangman android应用程序,我在注册输入的字母和更新游戏时遇到问题。我用屏幕上的键盘在编辑文本中输入了一个字母,但什么也没发生 下面是我正在使用的代码:Java Android Hangman游戏-用户输入,java,android,Java,Android,我目前正在创建一个Hangman android应用程序,我在注册输入的字母和更新游戏时遇到问题。我用屏幕上的键盘在编辑文本中输入了一个字母,但什么也没发生 下面是我正在使用的代码: // Setting up user input input = (EditText) findViewById(R.id.inputguess); input.setFocusable(true); input.setFocusableInTouchMode(true); //G
// 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)));
my 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() { ... })