0

我正在嘗試根據指南實施設置活動。我有一個EditTextPreference,我想保留,但不是允許用戶鍵入任何值,值應通過藍牙(我已準備好藍牙部分)。如何隱藏鍵盤並以編程方式更新EditTextPreference?

目前,當點擊EditTextPreference時,它顯示帶有OK和取消按鈕的編輯器彈出窗口。這就是我想要的,所以我可以處理OKCancel點擊事件。

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,但將值保存在數據庫中。

我希望有人和我有同樣的問題,因爲我無法在互聯網上找到任何解決方案。

回答

0

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); 
} 
} 
  • 爲了設定值到EDITTEXT使用的方法:

    editTextPreferences.setText("Your value"); 
    
  • 爲了節省只是一個值,可以因爲其更使用共享偏好靈活,但如果你想要保存更多的數據,並在數據庫中擁有所有數據,你可以使用SQLite數據庫。它們都保存本地值,因爲當應用程序卸載時,它們將被刪除。

  • +0

    感謝您的回答。第一個答案似乎沒有幫助。鍵盤仍在顯示。我處理了'EditTextPreference.setOnPreferenceClickListener',它調用'hideKeyboard()'方法,但它不隱藏鍵盤。 – Apostrofix

    +0

    您可以嘗試EditTextPreference.setOnPreferenceChangeListener(); – user3796978

    +0

    它仍然是一樣的。 'setOnPreferenceChangeListeer()'被調用,然後它調用'hideKeyboard()'方法,但它不隱藏鍵盤。 – Apostrofix

    0

    儘量按照您的活動的onCreate方法使代碼鍵盤,只彈出當用戶點擊一個EditText。在Android Mainfest.xmlactivity標籤

    要隱藏Keybord

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

    使用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); 
    
        }