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

后端 未结 5 1686
北荒
北荒 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.

    0 讨论(0)
  • 2021-01-12 03:11

    I had the same problem, for me, adding android:singleLine="true" in EditText did the trick

    0 讨论(0)
  • 2021-01-12 03:16

    Try this,

        ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    
        if (event != null && (actionId == EditorInfo.IME_ACTION_GO
                                || event.getKeyCode() == event.KEYCODE_ENTER))
        {
        //do whatever you want
        }
    
                    }
                });
    
    
    
    <EditText
        android:id="@+id/comment_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:maxLines="1"
        android:imeOptions="actionGo"
        /> 
    
    0 讨论(0)
  • 2021-01-12 03:17
    ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId==EditorInfo.IME_ACTION_DONE){
                //do something
            }
        return false;
        }
    });
    
    0 讨论(0)
  • 2021-01-12 03:20

    try this code this is a code with 2 fields in which i have user time feature. in first one keyboard shows next button and in second one soft keypad shows done option and on done option i hide the soft keypad.

    below is the xml file code.

      <EditText
                    android:id="@+id/txt1"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top|left"
                    android:imeOptions="actionNext"
                    android:inputType="text|textNoSuggestions"
                    android:text=""
                    android:visibility="visible" />
    
        <EditText
                    android:id="@+id/txt2"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top|left"
                    android:imeOptions="actionDone"
                    android:inputType="text|textNoSuggestions"
                    android:text=""
                    android:visibility="visible"
                    />
    

    now below is the java code: for hiding soft keypad on next and same can be performed for done option as well.

    this code add in onCreate() of your class.

        EditText first_txt = (EditText) findViewById(R.id.txt1);
        EditText second_txt = (EditText) findViewById(R.id.txt2);
    
        first_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
    
                    InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    
                    return true; // Focus will do whatever you put in the logic.
                }
                return false;  // Focus will change according to the actionId
            }
        });
    
    second_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
    
                    InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    
                    return true; // Focus will do whatever you put in the logic.
                }
                return false;  // Focus will change according to the actionId
            }
        });
    

    This code works for , hope it helps you out!

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