2014-01-24 59 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() { ... })