2014-01-24 33 views
0

我目前正在創建一個Hang子手android應用程序,並且在註冊輸入的字母和更新遊戲時遇到問題。我用屏幕鍵盤輸入一個字母到創建的EditText中,沒有任何反應。Android的Hang子手遊戲 - 用戶輸入

下面是我使用的代碼:

// 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> 

經過研究,我能想到的唯一原因是的類型聽衆被使用,但我不完全確定。任何幫助將不勝感激(讓我知道如果我應該添加更多的我的代碼)。

回答

1

實際上,OnKeyListener只能用於硬件鍵盤。要使用軟件(屏幕)鍵盤,您必須添加一個TextWatcher

editText.addTextChangedListener(new TextWatcher() { ... })