0

Need to make a loop that handles wrong answers, allowing another input to 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;
  }
});
KingBuzzo
  • 29
  • 7

2 Answers2

0

Use IME_ACTION instead of key listener like below

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

And use OnEditorActionListener on EditText like below

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

Refer this answer.

It'll help you.

ELITE
  • 5,815
  • 3
  • 19
  • 29
-1

Try removing return false; or put it above the parenthesis.

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;
                        }
                    }
                });
Alishah momin
  • 114
  • 1
  • 10