Android execute function after pressing “Enter” for EditText

不想你离开。 提交于 2019-11-30 11:48:20

From what I can see, It looks like you have the wrong import.

Try

edittext.setOnKeyListener(new View.OnKeyListener() {

OR add this import

import android.view.View.OnKeyListener;

and Remove this one

import android.content.DialogInterface.OnKeyListener;
Mohamed_AbdAllah

To receive a keyboard event, a View must have focus. To force this use:

edittext.setFocusableInTouchMode(true);
edittext.requestFocus();

After that continue with the same code in the example:

edittext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});

Delete the import statement that has DialogInterface, then import the View.OnKeyListener.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!