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