Handle “Enter” key on Jelly Bean

后端 未结 1 595
执念已碎
执念已碎 2021-01-23 18:38

I\'m making an application, in this application I have edit text. I want when user write some text in edit text end then press enter button, I want it call some command. This wh

相关标签:
1条回答
  • 2021-01-23 19:22

    This is a known bug that makes the Enter key not be recognized on several devices. A workaround to avoid it and make it work would be the following:

    Create a TextView.OnEditorActionListener like this:

    TextView.OnEditorActionListener enterKey = new TextView.OnEditorActionListener() {
      public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_GO) {
          // Do whatever you need
        }
        return true;
      }
    };
    

    Assuming your View is an EditText, for instance, you'd need to set it this way:

    final EditText editor = (EditText) findViewById(R.id.Texto);
    editor.setOnEditorActionListener(enterKey);
    

    The final step to go is assigning the following attribute to the EditText:

    android:imeOptions="actionGo"
    

    This basically changes the default behavior of the enter key, setting it to the actionGo IME option. In your handler simply assign it the listener you've created and this way you'll have the enter key behavior.

    0 讨论(0)
提交回复
热议问题