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

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

      
    
        
    

    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!

提交回复
热议问题