Android imeOptions=“actionDone” not working

前端 未结 8 2044
无人及你
无人及你 2020-12-16 09:14

I am trying to get a login screen for an Android app and so far this is my code:



        
相关标签:
8条回答
  • 2020-12-16 09:36

    Just add android:inputType="..." to your EditText. It will work!! :)

    0 讨论(0)
  • 2020-12-16 09:37

    You should set OnEditorActionListener for the EditText to implement the action you want to perform when user clicks the "Done" in keyboard.

    Thus, you need to write some code like:

    password.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Do whatever you want here
                return true;
            }
            return false;
        }
    });
    

    See the tutorial at android developer site

    0 讨论(0)
  • 2020-12-16 09:37

    just Add this to your attributes :

    android:inputType="textPassword"
    

    Doc: here

    0 讨论(0)
  • 2020-12-16 09:39

    Qianqian is correct. Your code only listens for the button click event, not for the EditorAction event.

    I want to add that some phone vendors may not properly implement the DONE action. I have tested this with a Lenovo A889 for example, and that phone never sends EditorInfo.IME_ACTION_DONEwhen you press done, it always sends EditorInfo.IME_ACTION_UNSPECIFIED so I actually end up with something like

    myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
      {
        boolean handled=false;
    
        // Some phones disregard the IME setting option in the xml, instead
        // they send IME_ACTION_UNSPECIFIED so we need to catch that
        if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId)
        {
          // do things here
    
          handled=true;
        }
    
        return handled;
      }
    });
    

    Also note the "handled" flag (Qianqian didn't explain that part). It might be that other OnEditorActionListeners higher up are listening for events of a different type. If your method returns false, that means you didn't handle this event and it will be passed on to others. If you return true that means you handled/consumed it and it will not be passed on to others.

    0 讨论(0)
  • 2020-12-16 09:48

    Just to add to Qianqian & peedee answers:

    Here's the reason why the action id is EditorInfo.IME_ACTION_UNSPECIFIED (0) for any enter event.

    actionId int: Identifier of the action. This will be either the identifier you supplied, or EditorInfo#IME_NULL if being called due to the enter key being pressed.

    (Source: Android documentation)

    So the proper way to handle enter key events would be:

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_NULL) {
            // Handle return key here
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-16 09:53

    After trying many things, this is what worked for me:

        android:maxLines="1"
        android:inputType="text"
        android:imeOptions="actionDone"
    
    0 讨论(0)
提交回复
热议问题