How to hide the keyboard and update EditTextPreference programmatically?

时间:2016-04-25 09:24:07

标签: android android-preferences android-settings edittextpreference

I am trying to implement the settings activity according to the guidelines. I have an EditTextPreference that I want to keep, but instead of allowing the user to type in any value, the value should come through Bluetooth (i have the Bluetooth part ready).

At the moment, when the EditTextPreference is clicked, it shows the editor popup with okay and cancel buttons. That's how I want it to be, so I can handle the OK or Cancel click events.

1) The first problem is that the keyboard also shows up - I don't want that, because the value should come from the background. I've added these properties, but nothing seems to have any effect on hiding the keyboard(even if I switch them around):

<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) The second problem is: how do I update the value of the EditTextPreference from the code behind, so the user doesn't have to type in anything, but just to see the value and click okay or cancel?

3) Third problem / question: is it okay to save the values in a database instead of using the shared preferences? Basically, I want to have the common settings UI, but keep the values in a database.

I hope that someone has had the same issues as me, because I was unable to find any solutions on the internet.

3 个答案:

答案 0 :(得分:0)

1.When you click the edit box, you should call this method, that hides the keyboard.

 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. For setting a value to the editText use the method:

     editTextPreferences.setText("Your value");
    
  2. For saving just a value, you can use Shared Preference because its more flexible, but if you want to save more data and to have all in a db you can use SQLite db. Both them save local values,because when app is uninstall, they are deleted.

答案 1 :(得分:0)

For updating EditTextPreference check this one EditTextPreference.setText(value) not updating as expected

For disable Input =>

setFocusableInTouchMode(boolean)
setFocusable(boolean)

答案 2 :(得分:0)

在您的活动的onCreate方法中尝试以下代码,以便在用户点击EditText时弹出键盘。

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

  

android:windowSoftInputMode="stateHidden"标记

下的Android Mainfest.xml中使用activity

隐藏键盘在onClick事件中调用此方法

 private void hideKeyboard() {

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

    }