【问题标题】:How to hide the keyboard and update EditTextPreference programmatically?如何隐藏键盘并以编程方式更新 EditTextPreference?
【发布时间】:2016-04-25 09:24:07
【问题描述】:

我正在尝试根据指南实施设置活动。我有一个我想保留的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,但将值保存在数据库中。

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

【问题讨论】:

    标签: android android-preferences android-settings edittextpreference


    【解决方案1】:

    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. 1234563它们都保存本地值,因为当应用程序卸载时,它们会被删除。

    【讨论】:

    • 感谢您的回答。第一个答案似乎没有帮助。键盘还在显示。我已经处理了EditTextPreference.setOnPreferenceClickListener,它调用了hideKeyboard() 方法,但它并没有隐藏键盘。
    • 你可以试试EditTextPreference.setOnPreferenceChangeListener();
    • 还是一样。调用setOnPreferenceChangeListeer(),然后调用hideKeyboard() 方法,但不隐藏键盘。
    【解决方案2】:

    要更新 EditTextPreference,请检查这个 EditTextPreference.setText(value) not updating as expected

    对于禁用输入 =>

    setFocusableInTouchMode(boolean)
    setFocusable(boolean)
    

    【讨论】:

      【解决方案3】:

      尝试在 Activity 的 onCreate 方法中使用以下代码,使键盘仅在用户单击 EditText 时弹出。

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

      Android Mainfest.xmlactivity 标签下使用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);
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 2023-04-03
        • 1970-01-01
        • 2018-08-12
        • 1970-01-01
        相关资源
        最近更新 更多