在错误的答案之后,EditText不会接受另一个输入?

时间:2017-07-13 04:52:20

标签: android android-edittext

需要制作一个处理错误答案的循环,允许另一个输入EditText

mEdit.setOnKeyListener(new View.OnKeyListener() {
   public boolean onKey(View v, int keyCode, KeyEvent event) {
      if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

         if(sumAsString.equals(mEdit.getText().toString())) {
            Toast.makeText(getApplicationContext(), "That's right!", Toast.LENGTH_SHORT).show();
         } else {
             mEdit.setText(null);
             Toast.makeText(getApplicationContext(), "Sorry Try again ", Toast.LENGTH_SHORT).show();
             return true;
         }
      }
      return false;
  }
});

2 个答案:

答案 0 :(得分:0)

使用IME_ACTION代替下面的关键监听器

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/prompt_password"
    android:imeActionId="@+id/login"
    android:imeOptions="actionDone"
    android:inputType="textPassword"
    android:maxLines="1"
    android:singleLine="true"/>

并在OnEditorActionListener上使用EditText,如下所示

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
           //do here your stuff f
           return true;
        }
        return false;
    } 
});

参考this answer

它会帮助你。

答案 1 :(得分:-1)

尝试删除return false;或将其放在括号上方。

mEdit.setOnKeyListener(new View.OnKeyListener() {
   public boolean onKey(View v, int keyCode, KeyEvent event) {
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                            if(sumAsString.equals(mEdit.getText().toString())) {
                                    Toast.makeText(getApplicationContext(), "That's right!", Toast.LENGTH_SHORT).show();
                            } else {
                                mEdit.setText(null);
                                Toast.makeText(getApplicationContext(), "Sorry Try again ", Toast.LENGTH_SHORT).show();

                                return true;

                            }
                            return false;
                        }
                    }
                });