Android imeOptions=“actionDone” not working

前端 未结 8 2068
无人及你
无人及你 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: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.

提交回复
热议问题