【问题标题】:Hide Android soft keyboard in Fragment when clicked on outside在外部单击时在 Fragment 中隐藏 Android 软键盘
【发布时间】:2016-04-27 11:58:55
【问题描述】:

我有一个包含用于输入的 EditText 的片段,但现在我想在用户单击 EditText 之外的屏幕时关闭键盘。

我知道如何在活动中执行此操作,但对于片段来说似乎有所不同。

我在 view.onTouchListener 上调用这个方法

public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

谁有解决办法,谢谢

【问题讨论】:

    标签: android android-fragments keyboard fragment


    【解决方案1】:

    在fragment的父Activity中重写如下方法:

     @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if ( v instanceof EditText) {
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                    v.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }
    

    并且在片段的布局中使用这个属性:

    android:focusableInTouchMode="true"
    

    希望这会对你有所帮助。

    【讨论】:

    • 这个答案是否帮助您解决了问题?
    • 它将适用于活动和活动的所有片段。你应该在你想要行为的所有布局中使用属性 android:focusableInTouchMode="true"。
    • 即使没有android:focusableInTouchMode="true" 也能正常工作。谢谢
    • 使用片段时没有调用函数dispatchTouchEvent,我确实在父活动中覆盖它并在片段布局上使用了android:focusableInTouchMode但没有成功
    【解决方案2】:

    使用此方法效果很好

    public static void hideKeyBoardMethod(final Context con, final View view) {
            try {
                view.post(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) con.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                });
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 我在 OnTouchListener 和 OnClickListener 两种方法@NIteshPareek 上尝试过但仍然存在同样的问题
    • 使用 dispatchTouchEvent();这个方法
    【解决方案3】:

    您可以使用此方法对我来说效果很好。 只需像这样传递布局的根元素的引用

    setupUI(rootView.findViewById(R.id.rootParent))
    

    setupUI 的代码如下..

    public void setupUI(View parentView) {
    
        //Set up touch listener for non-text box views to hide keyboard.
        if(!(view instanceof EditText)) {
    
            view.setOnTouchListener(new View.OnTouchListener() {
    
                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard();
                    handleCallBack();
                    return false;
                }
            });
        }
    

    【讨论】:

      【解决方案4】:

      我只是在activity的所有片段中重写了dispatchTouchEvent()方法。

      @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
          // TODO Auto-generated method stub
           if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                  View v = getCurrentFocus();
                  if ( v instanceof EditText) {
                      Rect outRect = new Rect();
                      v.getGlobalVisibleRect(outRect);
                      if (!outRect.contains((int)ev.getRawX(), (int)ev.getRawY())) {
                          v.clearFocus();
                          InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                          imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                      }
                  }
              } 
      

      并在主布局中片段的每个 XML 中设置属性

      android:focusableInTouchMode="true"
      

      【讨论】:

        【解决方案5】:

        你也可以这样做

        getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        );
        

        希望对您有所帮助!

        【讨论】:

          【解决方案6】:

          如果片段使用下面的代码

          View view = inflater.inflate(R.layout.fragment_test, container, false);
          
              view.setOnTouchListener(new View.OnTouchListener() {
                      public boolean onTouch(View v, MotionEvent event) {
          
                          if(event.getAction() == MotionEvent.ACTION_MOVE){
                              dispatchTouchEvent(event);
                          }
                          return true;
                      }
              });
          
          //here the rest of your code
          
          return view;
          

          应用此代码并调用 dispatchTouchEvent();方法

          【讨论】:

            【解决方案7】:

            如果你想在editText之外触摸,那么键盘隐藏自动所以使用这个代码

            public boolean dispatchTouchEvent(MotionEvent event) {
            
                    View view = getCurrentFocus();
                    boolean ret = super.dispatchTouchEvent(event);
            
                    if (view instanceof EditText) {
                        View w = getCurrentFocus();
                        int location[] = new int[2];
                        w.getLocationOnScreen(location);
                        float x = event.getRawX() + w.getLeft() - location[0];
                        float y = event.getRawY() + w.getTop() - location[1];
                        if (event.getAction() == MotionEvent.ACTION_DOWN
                                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                        }
                    }
                    return ret;
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-12-24
              • 1970-01-01
              • 1970-01-01
              • 2022-12-14
              • 2012-10-30
              • 2015-09-04
              • 2014-07-19
              相关资源
              最近更新 更多