onKeyUp函数没有调用第二次

时间:2016-09-16 21:24:58

标签: android onkeyup

我一直在尝试基于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;
}

和我的codeTurn函数代码是

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;
}

1 个答案:

答案 0 :(得分:0)

Android软键盘很少使用关键事件。使用Android软键盘的正确方法是通过InputConnection。只有硬件密钥通常会发出密钥事件。基本上你是以正确的方式为Windows或网络编写代码,但对Android来说是错误的方式。