【问题标题】:Hide keyboard when switching tabs in android在android中切换标签时隐藏键盘
【发布时间】:2015-02-06 05:02:36
【问题描述】:

我已经创建了带有自定义类的选项卡条,并且在每个选项卡中显示了一个片段。当键盘打开并且我切换到选项卡时,第二个片段被调用但键盘没有隐藏。

我在两个片段的 onCreateView() 中都使用了下面的代码,但它不起作用:

//To Hide Soft 
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

【问题讨论】:

    标签: android android-fragments android-softkeyboard android-tabs


    【解决方案1】:

    onCreateView() 中使用该代码的问题是,每当在父片段/活动中创建选项卡时,都会创建在选项卡中初始化的片段。我对选项卡中片段的行为进行了一些调查,并意识到您在覆盖许多生命周期方法(例如 onViewCreated()onResume() 等)时会遇到同样的问题。

    我发现解决此问题的最佳方法是在您希望隐藏键盘的片段中覆盖setUserVisibleHint(boolean isVisibleToUser)。每当片段的可见性发生变化时,都会调用此方法。

    @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isVisibleToUser) {
                try {
                    InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    mImm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
                    mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                } catch (Exception e) {
                    Log.e(TAG, "setUserVisibleHint: ", e);
                }
            }
        }
    

    【讨论】:

    • 谢谢,这在切换时在标签片段中对我有用
    • setUserVisibleHint() 已经被弃用了一段时间。您可能只想将逻辑移到 onResume() 而不是
    【解决方案2】:

    使用此类在运行时隐藏和显示键盘。尝试调用 onTabChangedListener 上的方法。希望对您有所帮助。

    public class KeyBoardHandler {
    
        public static void hideSoftKeyboard(Activity activity) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    
        public static void showSoftKeyboard(Activity activity) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    }
    

    【讨论】:

    • 回答未提出的问题。
    【解决方案3】:

    将此代码放在片段的onDestroy方法中。

    try {
            InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
            mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    
        } catch (Exception e) {
    
        }
    

    【讨论】:

      【解决方案4】:

      试试下面的代码

      界面: ICallBacks

      public interface ICallBacks {
        public void isChanged();
      }
      

      在您的活动中定义变量,如

      public ICallBacks mCallbacks;
      

      在 OnPageChangeListener 中

       @Override
      public void onPageScrolled(int arg0, float arg1, int arg2) {
      
      
          if (mCallbacks != null)
              mCallbacks.isChanged();
      }
      

      在您的片段中,您需要使用 ICallBacks 接口实现

      @Override
      public void onAttach(Activity activity) {
          // TODO Auto-generated method stub
          super.onAttach(activity);
          if (activity != null) {
              ((PagerActivity) getActivity()).mCallbacks = this;
          }
      
      }
      @Override
      public void isChanged() {
      
          if (isVisible())
              hideKeyboard();
      }
      
          private void hideKeyboard() {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            View view = this.getCurrentFocus();
            if (view != null) {
              inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
              }
          }
      

      【讨论】:

      • 我这里试过:public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView=inflater.inflate(R.layout.fragment_route_audit, container, false); hideKeyboard(rootView); ((SideDrawerActivity)getActivity()).setTitle("路由审计");绑定视图(根视图);返回根视图; }
      • @user3138859 - 尝试在每个片段的 onDestroyView 中隐藏键盘
      • 片段可见时是否启用键盘?
      • @user3138859 - 当键盘在你的情况下可见?
      • 当我在edittext中输入值然后键盘打开...然后假设我移动到另一个选项卡...第二个片段正在被调用..即使这样键盘仍然打开
      猜你喜欢
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多