setOnEditorActionListener not working with soft keyboard submit button, but does with laptop Enter key?

后端 未结 5 1694
北荒
北荒 2021-01-12 02:47

Can someone please provide a solution to get a working listener for the soft-keyboard DONE button, and/or explain what I\'m doing wrong in my current approach?<

5条回答
  •  执念已碎
    2021-01-12 02:59

    This is an old issue that seems not to be solved yet. There's a workaround for these cases. Simply take the EditText (or whatever View you might be needing this and assign it a onEditorActionListener, like this:

    final EditText editor = (EditText) findViewById(R.id.myEditText);
    editor.setOnEditorActionListener(EnterOnText);
    

    Now define the EnterOnText implementation like this:

    TextView.OnEditorActionListener EnterOnText = new TextView.OnEditorActionListener() {
      public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_GO) {
          final String msg = view.getText().toString();
          if (!msg.isEmpty()) {
            // Do whatever you need here
            ...
          }
        }
        return true;
      }
    };
    

    Now, simply change the layout's imeOptions attribute to:

    android:imeOptions="actionGo"
    

    That will, oppositely, probably make your emulator's enter key not work, but will (should) do on all physical devices.

提交回复
热议问题