Search code examples
javaandroidkeyboardkeypressonkeypress

I can't do the key enter Go


I am beginner in android but not in Java , and have a problem.

Took hours losses in my application ignores keyboard to enter and you put the code below. What I want is that when I do a thing to enter and go. Since it seems redundant and have not optimal OK button when you have an OK in the Enter itself.

    t_Num = (EditText) findViewById(R.id.eTT);
    ...
    t_Num.setFocusableInTouchMode(true);
    t_Num.requestFocus();
    t_Num.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            System.out.println("entra en Onkey" + event.toString());
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)){
                    tV.setText("OK");
                    System.out.println("Press OK");

                    return true;
                }
            return false;
        }
    });

Whit this code i get spam the keypress , but not spam the keypress OK.

And XML:

<EditText
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned|phone"
    android:digits="-0123456789"
    android:ems="10"
    android:id="@+id/eTT"
    android:imeActionLabel="@string/OK"
    android:maxLength="6"
    android:maxLines="1"
    android:layout_alignBottom="@+id/tV2_1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@+id/btnOk"
    android:layout_toEndOf="@+id/btnOk" />

thanks for help
PD : I search in this page but not get solution , font1 , font2 , etc...
I use search :)


Solution

  • Try this:

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    //your code
                    return true;
                }
                return false;
            }
        });
    

    This is if the editText has

    android:imeOptions="actionGo"
    

    in its xml. Same should apply with Enter. Just change the EditorInfo action.

    EditText is a subclass of TextView.