簡體   English   中英

如何隱藏鍵盤並以編程方式更新EditTextPreference?

[英]How to hide the keyboard and update EditTextPreference programmatically?

我正在嘗試根據指南實施設置活動。 我有一個想保留的EditTextPreference ,但是該值應該通過Bluetooth來傳遞(我已准備好Bluetooth部分),而不是允許用戶鍵入任何值。

此刻,當單擊EditTextPreference時,它會顯示帶有確定和取消按鈕的編輯器彈出窗口。 這就是我想要的方式,因此我可以處理“ OK或“ Cancel單擊事件。

1)第一個問題是鍵盤也會顯示-我不希望那樣,因為該值應該來自背景。 我添加了這些屬性,但似乎對隱藏鍵盤沒有任何影響(即使我將它們切換了一下):

<EditTextPreference
    android:selectable="true"
    android:enabled="true"
    android:editable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:cursorVisible="false"
    android:capitalize="words"
    android:inputType="none"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:title="@string/pref_title_id" />

2)第二個問題是:如何從后面的代碼中更新EditTextPreference的值,因此用戶不必鍵入任何內容,而只需查看該值並單擊“確定”或“取消”?

3)第三個問題:是否可以將值保存在數據庫中而不使用共享首選項? 基本上,我希望擁有通用的設置UI,但要將值保留在數據庫中。

我希望有人遇到了與我相同的問題,因為我無法在互聯網上找到任何解決方案。

1.單擊編輯框時,應調用此方法,該方法將隱藏鍵盤。

 private void hideKeyboard() {
    View view = getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
 }
}
  1. 要為editText設置值,請使用方法:

      editTextPreferences.setText("Your value"); 
  2. 為了只保存一個值,可以使用“共享首選項”,因為它更靈活,但是如果要保存更多數據並將其全部包含在db中,則可以使用SQLite db。 它們都保存本地值,因為在卸載應用程序時會刪除它們。

要更新EditTextPreference,請檢查此EditTextPreference.setText(value)是否未按預期更新

對於禁用輸入=>

setFocusableInTouchMode(boolean)
setFocusable(boolean)

嘗試在活動的onCreate方法中遵循以下代碼,以使僅當用戶單擊EditText時才彈出鍵盤。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

要么

activity標簽下的Android Mainfest.xml使用android:windowSoftInputMode="stateHidden"

要隱藏鍵盤,請在onClick事件中調用此方法

 private void hideKeyboard() {

            View view = getCurrentFocus();
            if (view != null) {
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                        hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }

暫無
暫無

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

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