簡體   English   中英

按下后隱藏軟鍵盤

[英]Hide soft keyboard on pressing back

我在Activity有一個EditText ,我想讓它處於活動狀態,當我打開Activity時,軟鍵盤會打開。 這是我的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" />

我已經使用android:windowSoftInputMode="stateVisible"作為我有這個EditText的活動。

問題是,當我按back一次,鍵盤不會隱藏(理想情況下它在所有其他EditText S),當我按back一遍,它關閉了Activity 在第一次back ,我沒有接到onBackPressed()的電話,而在第二次back ,我這樣做。 為什么會發生這種行為以及如何解決?

編輯我想要的是,如果鍵盤打開,按下鍵應關閉鍵盤,如果鍵盤未打開,則關閉活動。

嘗試這個 ...

創建名為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()

我在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);
        }
    }
}

試試這樣,

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

這可能對你有所幫助

根據您的要求

如果鍵盤打開,按下鍵應關閉鍵盤,如果鍵盤未打開,則關閉活動

我測試過我的代碼工作正常。

第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步:在您的活動中初始化CustomEditText並隱藏KeyBoard,如下所示。

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

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

第4步:在您的活動中簡單地Override onBackPressed()方法。

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM