【问题标题】:Hide soft keyboard on pressing back按下后隐藏软键盘
【发布时间】:2017-06-13 18:21:21
【问题描述】:

我在 Activity 中有一个 EditText,我希望它处于活动状态,并且当我打开 Activity 时软键盘处于打开状态。这是我的xml EditText

<EditText
    android:background="@null"
    android:cursorVisible="true"
    android:elegantTextHeight="true"
    android:enabled="true"
    android:focusable="true"
    android:hint="Search"
    android:id="@+id/editText11"
    android:inputType="textNoSuggestions|textCapSentences"
    android:layout_centerVertical="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:singleLine="true"
    android:textColor="#000000"
    android:textCursorDrawable="@null" />

我已将android:windowSoftInputMode="stateVisible" 用于我拥有此EditText 的活动。

问题是,当我按一次back 时,键盘不会隐藏(理想情况下它会在所有其他EditTexts 中隐藏),当我再次按back 时,它会关闭Activity。在第一次back 按下时,我没有接到onBackPressed() 的电话,而在第二次back 按下时,我会。为什么会出现这种行为以及如何解决?

编辑我想要的是,如果键盘打开,按回应该关闭键盘,如果键盘没有打开,则关闭活动。

【问题讨论】:

  • 我更新我的答案创建一个示例项目尝试理解然后在你的主项目中实施。
  • @Harshad 好的,我会尝试并告诉你。

标签: android android-edittext android-manifest android-softkeyboard


【解决方案1】:

试试这个...

创建名为 Util 的类并放置此代码

public static void hideSoftKeyboard(Activity activity) {
    final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (inputMethodManager.isActive()) {
        if (activity.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

并调用Activity的onBackPressed()

【讨论】:

    【解决方案2】:

    我在我的 BaseActivity.java 中添加以下代码

    @Override
    protected void onPause() {
        super.onPause();
    
        final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && inputMethodManager.isActive()) {
            if (getCurrentFocus() != null) {
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这样试试,

      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (event.getAction() == KeyEvent.ACTION_DOWN) {
              switch (keyCode) {
                  case KeyEvent.KEYCODE_BACK:
                       //useful for hiding the soft-keyboard is:
                       getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      
                      return true;
              }
      
          }
          return super.onKeyDown(keyCode, event);
      }
      

      这可能对你有帮助

      【讨论】:

        【解决方案4】:

        根据您的要求

        如果键盘是打开的,按回应该关闭键盘,如果 键盘没有打开,然后关闭activity

        我已经测试过我的代码可以正常工作。

        第 1 步:创建CustomEditText,如下所示。

         <com.yourpackage.yourappname.CustomEditText
                android:id="@+id/edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        

        第 2 步:创建CustomEditText.java 类。

        public class CustomEditText extends EditText {
        
            Context context;
            private static Activity mSearchActivity;;
        
            public CustomEditText(Context context, AttributeSet attrs) {
                super(context, attrs);
                this.context = context;
            }
        
            @Override
            public boolean dispatchKeyEventPreIme(KeyEvent event) {
                if(mSearchActivity != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
        
                    if(state != null){
                        if(event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0){
        
                            InputMethodManager mgr = (InputMethodManager)
                                    context.getSystemService(Context.INPUT_METHOD_SERVICE);
                            mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
                        }
                        else if(event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled() && state.isTracking(event)){
                            mSearchActivity.onBackPressed();
                            return true;
                        }
                    }
                }
        
        
                return super.dispatchKeyEventPreIme(event);
            }    
        
        }
        

        第 3 步:在您的 Activity 中初始化 CustomEditText 并隐藏 KeyBoard,如下所示。

        CustomEditText editText = (CustomEditText) findViewById(R.id.edittext);
        
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        

        第 4 步:在您的 Activity 中只需使用 Override onBackPressed() 方法。

         @Override
            public void onBackPressed() {
                super.onBackPressed();
        
            }
        

        【讨论】:

        • 我不想隐藏软键盘并立即关闭活动。我想要的是,如果键盘打开,按回应该关闭键盘,如果键盘没有打开,则关闭活动。
        • @AmitTiwari 请参阅更新我的答案。
        • 从哪里调用这个函数?
        • @AmitTiwari 你必须重写。
        • @AmitTiwari 查看我的完整答案。
        猜你喜欢
        • 2011-08-29
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-06-08
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多