簡體   English   中英

如何在EditText聚焦時禁用鍵盤顯示?

[英]How to disable keyboard showing when EditText focused?

需要打開附加到一個視圖的鍵盤並將鍵事件委托給edittext,但是在聚焦時不需要將鍵盤附加到EditText。 我試過這段代碼:

在清單中:

<activity android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />

碼:

InputMethodManager inputMethodManager = (InputMethodManager) rootView.getContext()
                                   .getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);

view.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
        switch (keyEvent.getAction()){
            case KeyEvent.ACTION_UP:
                editText.onKeyUp(keycode, keyEvent);
                break;
            case KeyEvent.ACTION_DOWN:
                editText.onKeyDown(keycode, keyEvent);
                break;
        }
        return true;
    }
});

一切正常(鍵盤正在顯示),但是當edittext獲得焦點時 - 所有邏輯都被破壞(鍵盤在edittext上重新顯示);

editext.requestFocus();

專注於編輯處理選擇所需的文本以及依賴於焦點的其他事物。

任何幫助對我來說都很重要,謝謝;

更新:沒有必要隱藏鍵盤,所需的步驟:

  1. 鍵盤打開並附加到某些視圖
  2. 編輯文本獲得焦點,但鍵盤沒有獲得它

如果這是您正在使用的活動,則可以使用清單中的以下行來隱藏創建時的鍵盤。

        <activity
        android:name=".YourActivityName"
        android:windowSoftInputMode="stateHidden"></activity>

創建一個功能:

/* Hides keyboard, if diaplayed */
public void hideKeyboard(){
    View currentFocus = MainAcitivty.this.getCurrentFocus();    // Change the name according to your activity's name.
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(MainActivity.this.INPUT_METHOD_SERVICE);
    if(currentFocus != null){
        inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(),0);
        currentFocus.clearFocus();
    }
}

並從您想要隱藏鍵盤的位置調用上述功能。

希望這可以幫助...

暫無
暫無

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

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