如何在焦点更改时保持键盘打开,但不关闭并重新打开

时间:2016-02-06 19:01:39

标签: android android-softkeyboard

我有OnFocusChangeListener

    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            InputMethodManager myIMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            if(hasFocus) {
                myIMM.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
            } else {
                myIMM.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    };

我将此Listener设置为我以编程方式创建的每个EditText。有效,但有时不行。它提供了很多这样的警告:

W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection

有时我可以看到键盘关闭并重新打开。 所以,我认为这个问题是因为我多次打开和关闭键盘。如何保存键盘状态?因此,如果新焦点是EditText,则键盘不会关闭。

抱歉英语不好,希望你能理解。

1 个答案:

答案 0 :(得分:1)

问题在于你正在做的事情,它必然会发生。您正在进行两次三次异步调用。 myImm.toggleSoftInput和hideSoftInput几乎同时被两个不同的视图调用:

  1. 正在获得焦点和
  2. 即释放焦点。
  3. 现在这个调用通过服务进行,并且无法保证首先通过输入方法接收哪个调用,因此行为是随机的。

    现在,如果您希望键盘在视图聚焦时打开,那么您只需要执行此操作

    InputMethodManager myIMM = 
        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            myIMM.showSoftInput(view, 2);
    

    如果您尝试在不自动打开键盘的视图的焦点上打开键盘,但是如果您只使用编辑文本,那么我不需要以上所有代码,我不认为您甚至想要这个监听器。

    如果您需要隐藏键盘,我建议您执行以下操作:

    1. 创建处理程序并向其发送延迟的请求。
    2. 从中删除旧邮件并保留最新请求。
    3.  
       private static class SearchHandler extends Handler {
          private WeakReference<CitySelectionActivity> mTarget;
      
          SearchHandler(CitySelectionActivity target) {
            mTarget = new WeakReference<>(target);
          }
      
          public void setTarget(CitySelectionActivity target) {
            mTarget.clear();
            mTarget = new WeakReference<>(target);
          }
      
          @Override
          public void handleMessage(final Message msg) {
            if (msg.what == 0) {
              InputMethodManager myIMM = (InputMethodManager) mTarget.get().getSystemService(Context.INPUT_METHOD_SERVICE);
              myIMM.showSoftInput((View)msg.obj,0);
            }
            if(msg.what ==1){
              InputMethodManager myIMM = (InputMethodManager) mTarget.get().getSystemService(Context.INPUT_METHOD_SERVICE);
              myIMM.hideSoftInputFromWindow(((View)msg.obj).getWindowToken(),0);
            }
          }
        }
      
        View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            Message message = new Message();
            message.obj = v;
            if (hasFocus) {
              message.what = 0;
              handler.removeMessages(0);
              handler.removeMessages(1);
              handler.sendMessageDelayed(message, 200);
            } else {
              message.what = 1;
              handler.removeMessages(0);
              handler.removeMessages(1);
              handler.sendMessageDelayed(message, 200);
            }
          }
        };