EditText input method action not working when setting imeActionLabel

后端 未结 4 413
青春惊慌失措
青春惊慌失措 2021-01-18 07:13

I have an Edittext with imeoptions asactiongo. and I triggered my event when pressing soft keyboard enter button.

mModelId.setOnE         


        
4条回答
  •  有刺的猬
    2021-01-18 07:51

    setImeActionLabel take two parameters and the second int parameter should be one of the those that are in the EditorInfo class. Such as:

        EditorInfo.IME_ACTION_GO
        EditorInfo.IME_ACTION_DONE
        EditorInfo.IME_ACTION_NEXT
        ....
    

    You cannot send there any other integer like KeyEvent.KEYCODE_ENTER

    And you have to set bothimeOptions parameter and singleLine parameter in XML in order it to work. Example:

    
    

    Here is the code that I used and it is working:

    XML Layout:

    
    
        
    
    
    

    And the basic Activity code:

        mEditText2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_GO) {
    
                    Toast.makeText(MainActivity.this, "You entered " + v.getText().toString(), Toast.LENGTH_LONG).show();
    
                    handled = true;
                }
                return handled;
            }
        });
    
        mEditText2.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO);
    

提交回复
热议问题