Android enter key listener

22,650

Solution 1

you can use this to listen to keyboard events

http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener.html

Solution 2

In your EditText you should specify keyboard action using imeOptions.

<EditText
       android:id="@+id/query"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:imeOptions="actionGo"
       android:inputType="text" />

And in your Activity's class:

 EditText editText = (EditText) findViewById(R.id.query);
 editText.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_GO) {

                        return true;
                    }
                    return false;
                }
            });

Solution 3

If you want to catch user press Enter register onKeyListener on your Edittext

  yourEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode==KeyEvent.KEYCODE_ENTER) { //Whenever you got user click enter. Get text in edittext and check it equal test1. If it's true do your code in listenerevent of button3
                    if("test1".equals(edt.getText().toString())) {
                        //paste your code in button3 listener here
                        }
                }

}
    )

This part is wrong.

if(editext1.getText().toString().equals.null)) { testwrong.setText("Wrong");

you should change to

if (editext1.getText().toString() != null && !editext1.getText().toString().isEmpty()) {
  // doSomething
}
Share:
22,650
C0dexe
Author by

C0dexe

Updated on July 25, 2022

Comments

  • C0dexe
    C0dexe almost 2 years

    Can someone help me with a softkeyboard enter key listener?

    I need a enter key listener like a button listener that would have a few editext listeners inside like this

    enterkey.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) { 
            if(editext1.getText().toString().equalsIgnoreCase("test1")) {
                button3.performClick();
            }
            if(editext1.getText().toString().equalsIgnoreCase("test2")) {
                button4.performClick();
            }
        }
    );
    

    I also need to know if something like this is correct?

    if(editext1.getText().toString().equals.null)) {
           testwrong.setText("Wrong"); 
    

    I have now tried using this code but keep getting a null value when I hit enter? Can anyone suggest a solution to avoid this?

    editext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                if ("test1".equalsIgnoreCase(anstext.getText().toString())) {
                    but4.performClick();
                }
            } else if ("test2".equalsIgnoreCase(editext.getText().toString())) {
                but5.performClick();
            }
            if ("test5".equalsIgnoreCase(editext.getText().toString())) {
                but6.performClick();
            }
            if ("test7".equalsIgnoreCase(editext.getText().toString())) {
                but7.performClick();
            }
            if (editext.getText().toString() != null) {
                testwrong.seText("wrong");
            }
            return true;
        }
    });