Search code examples
androidbuttonandroid-edittextonclicklistener

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

Solution

  • you can use this to listen to keyboard events

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