onKeyUp 函数不调用第二次

onKeyUp function not calling second time

我一直在尝试构建一个基于 GHOST 游戏的应用程序。 我写了一个 onKeyUp 函数,它只接受小写字母并将其添加到一个名为 wordfragment 的字符串中,然后在其中调用函数 computerTurn。但是我在第一次成功 运行 之后看到,即调用 computerTurn 函数并从 computerturn 函数获取 return 语句它(onkeyup)第二次不起作用。 这是我的 onKeyUp 函数代码。

@Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
    char ch = (char)event.getUnicodeChar();
    if( !( ch >= 'a' && ch <='z'  ) ) {
        return super.onKeyUp(KeyCode, event);
    }
    wordFragment = wordFragment + ch;
    label.setText(COMPUTER_TURN);
    text.setText(wordFragment);

    userTurn = false;
    computerTurn();
    return true;
}

我的 computerTurn 函数代码是

private boolean computerTurn() {
    if(wordFragment.length() >= 4 && dictionary.isWord(wordFragment)){
        label.setText("Computer wins");
       // challenge.setEnabled(false);
        return true;
    }
    else {
        String word = dictionary.getAnyWordStartingWith(wordFragment.toLowerCase());
        if(word!=null){
            Toast.makeText(GhostActivity.this, "comp word found", Toast.LENGTH_SHORT).show();
            wordFragment += word.charAt(wordFragment.length());
        }
        else{
            Toast.makeText(GhostActivity.this, "comp word not found", Toast.LENGTH_SHORT).show();
            label.setText("User Wins!!");
            //challenge.setEnabled(false);
           // wordFragment += (char)(random.nextInt(26) + 61);
        }
    }
    // Do computer turn stuff then make it the user's turn again
    userTurn = true;
    label.setText(USER_TURN);
    text.setText(wordFragment);
    Toast.makeText(GhostActivity.this, "return true", Toast.LENGTH_SHORT).show();
    return true;
}

Android软键盘很少使用按键事件。使用 android 软键盘的正确方法是通过 InputConnection。一般只有硬件按键才会发出按键事件。基本上,您为 Windows 或网络编写了正确的代码,但为 Android.

编写了错误的代码