0

我正在尝试根据指南实施设置活动。我有一个EditTextPreference我想保留的,但不是允许用户输入任何值,而是该值应该通过蓝牙来(我已经准备好蓝牙部分)。

目前,当EditTextPreference单击 时,它会显示带有确定和取消按钮的编辑器弹出窗口。这就是我想要的样子,所以我可以处理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,但将值保存在数据库中。

我希望有人和我有同样的问题,因为我无法在互联网上找到任何解决方案。

4

3 回答 3

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

    }
于 2016-04-25T10:03:00.173 回答
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);
 }
}
  1. 要为 editText 设置值,请使用以下方法:

     editTextPreferences.setText("Your value");
    
  2. 为了只保存一个值,您可以使用 Shared Preference 因为它更灵活,但如果您想保存更多数据并将所有数据保存在一个数据库中,您可以使用 SQLite db。它们都保存本地值,因为当应用程序被卸载时,它们会被删除。

于 2016-04-25T09:42:24.853 回答
0

要更新 EditTextPreference,请检查此 EditTextPreference.setText(value) not updated as expected

对于禁用输入 =>

setFocusableInTouchMode(boolean)
setFocusable(boolean)
于 2016-04-25T09:43:00.353 回答