简体   繁体   English

按下后隐藏软键盘

[英]Hide soft keyboard on pressing back

I have an EditText in an Activity and I want it to be active and soft-keyboard be open when I open that Activity . 我在Activity有一个EditText ,我想让它处于活动状态,当我打开Activity时,软键盘会打开。 Here is my xml for EditText : 这是我的EditText xml

<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" />

and I have used android:windowSoftInputMode="stateVisible" for the activity in which I have this EditText . 我已经使用android:windowSoftInputMode="stateVisible"作为我有这个EditText的活动。

The problem is, when I press back once, the keyboard does not hide(ideally it does in all other EditText s) and when I press back again, it closes the Activity . 问题是,当我按back一次,键盘不会隐藏(理想情况下它在所有其他EditText S),当我按back一遍,它关闭了Activity On the first back press, I am not getting a call to onBackPressed() while on the second back press, I do. 在第一次back ,我没有接到onBackPressed()的电话,而在第二次back ,我这样做。 Why is this kind of behaviour is happening and how to resolve it? 为什么会发生这种行为以及如何解决?

Edit What I want is, if keyboard is open, pressing back should close the keyboard and if the keyboard is not open, then close the activity. 编辑我想要的是,如果键盘打开,按下键应关闭键盘,如果键盘未打开,则关闭活动。

Try this ... 尝试这个 ...

create class called Util and put this code 创建名为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);
        }
    }
}

and call on the onBackPressed() of Activity 并调用Activity的onBackPressed()

I add below code in my BaseActivity.java 我在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);
        }
    }
}

Try like this, 试试这样,

@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);
}

This may helps you 这可能对你有所帮助

As Your Requirement : 根据您的要求

if keyboard is open, pressing back should close the keyboard and if the keyboard is not open, then close the activity 如果键盘打开,按下键应关闭键盘,如果键盘未打开,则关闭活动

I Have tested my code is work fine. 我测试过我的代码工作正常。

Step 1 : Create CustomEditText like below. 第1步:创建CustomEditText如下所示。

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

Step 2 : Create CustomEditText.java class. 第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);
    }    

}

Step 3 : In Your Activity intialize the CustomEditText and hide KeyBoard like below. 第3步:在您的活动中初始化CustomEditText并隐藏KeyBoard,如下所示。

CustomEditText editText = (CustomEditText) findViewById(R.id.edittext);

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Step 4: In Your Activity simply Override the onBackPressed() method. 第4步:在您的活动中简单地Override onBackPressed()方法。

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM