Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android enter key listener

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;
    }
});
like image 571
C0dexe Avatar asked Oct 17 '12 15:10

C0dexe


People also ask

How do I find my Enter key on Android?

myTextView. setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (event. getAction() == KeyEvent. KEYCODE_ENTER) { // Handle pressing "Enter" key here handled = true; } return handled; } });

Which listener is called for the device register the Enter key press?

Android Event Listeners This method is called when the user touches or focuses on the item using navigation-keys or trackball or presses on the "enter" key or presses down on the trackball.

How do I get the Enter button on my Keyboard?

Assuming your keyboard is anything like mine: Try tapping the key bottom left till you get the ? 123 setting, then tap the upward-arrow key above it. Your emoticon key bottom right should then change to Enter.


1 Answers

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;
                }
            });
like image 105
Sabre Avatar answered Oct 06 '22 13:10

Sabre