Is there any way to specify the input method type for android.support.v7.preference.EditTextPreference
?
5 回答
如果您不想使用第三方库,可以为 EditTextPreference 指定布局
<EditTextPreference android:defaultValue="0"
android:key="some_key"
android:title="Title"
android:dialogLayout="@layout/preference_edit_text"/>
然后在 res/layout/preference_edit_text.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="@android:id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="21dp"
android:layout_marginEnd="21dp"/>
</android.support.constraint.ConstraintLayout>
请注意,编辑文本 id 必须是 :@android:id/edit
但是您可以在该android:inputType
字段内自由使用任何您想要的内容
我确信有更好的方法来对齐 EditText 而不是使用21dp
边距,但至少它有效
现在可以使用Android-Support-Preference-V7-Fix库。
FixedEditTextPreference
将 XML 属性(如inputType
)转发到EditText
,就像原始首选项一样。
这是我的 Cory Charlton 答案版本,已转移到 Jetpack 偏好设置并用 Kotlin 编写:
import android.content.Context
import android.content.SharedPreferences
import android.text.InputType
import android.util.AttributeSet
import androidx.preference.EditTextPreference
class EditIntegerPreference : EditTextPreference {
constructor(context: Context?) : super(context) {
setInputMethod()
}
constructor(context: Context?, attributeSet: AttributeSet?) : super(context, attributeSet) {
setInputMethod()
}
constructor(context: Context?, attributeSet: AttributeSet?, defStyle: Int) : super(
context,
attributeSet,
defStyle
) {
setInputMethod()
}
override fun getText(): String =
try {
java.lang.String.valueOf(sharedPreferences.getInt(key, 0))
} catch (e: Exception) {
"0"
}
override fun setText(text: String?) {
try {
if (text != null) {
sharedPreferences?.edit()?.putInt(key, text.toInt())?.apply()
summary = text
} else {
sharedPreferences?.remove(key)
summary = ""
}
} catch (e: Exception) {
sharedPreferences?.remove(key)
summary = ""
}
}
override fun onSetInitialValue(defaultValue: Any?) {
val defaultValueInt: Int =
when (defaultValue){
is Int -> defaultValue
is String -> try {defaultValue.toInt()} catch (ex: java.lang.Exception){0}
else -> 0
}
text = sharedPreferences.getInt(key, defaultValueInt).toString()
}
private fun setInputMethod() {
setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
}
}
fun SharedPreferences.remove(key: String) = edit().remove(key).apply()
}
编辑:下面的先前答案是建立在股票上的android.preference.EditTextPreference
,不幸的是不适用于android.support.v7.preference.EditTextPreference
.
在控件中android.preference.EditTextPreference
以EditText
编程方式创建,并将AttributeSet
fromPreference
传递给它。
android.preference.EditTextPreference
资源:
public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mEditText = new EditText(context, attrs);
// Give it an ID so it can be saved/restored
mEditText.setId(com.android.internal.R.id.edit);
/*
* The preference framework and view framework both have an 'enabled'
* attribute. Most likely, the 'enabled' specified in this XML is for
* the preference framework, but it was also given to the view framework.
* We reset the enabled state.
*/
mEditText.setEnabled(true);
}
白色允许我们inputType
在Preference
自身上设置 并让它传递给EditText
. 不幸的是,android.support.v7.preference.EditTextPreference
似乎EditText
在Layout
有关解决此问题的想法,请参阅此问题:
只是想让您知道子类化 EditTextPreferenceDialogFragment 和覆盖 onAddEditTextToDialogView 以及覆盖 PreferenceFragmentCompat#onDisplayPreferenceDialog 以根据需要显示该子类似乎工作正常,感谢您的帮助。
创建您自己的扩展类EditTextPreference
并将其设置在那里。
这是我的EditIntegerPreference
课:
public class EditIntegerPreference extends EditTextPreference {
public EditIntegerPreference(Context context) {
super(context);
}
public EditIntegerPreference(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public EditIntegerPreference(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
getEditText().setSelectAllOnFocus(true);
}
@Override
public String getText() {
try {
return String.valueOf(getSharedPreferences().getInt(getKey(), 0));
} catch (Exception e) {
return getSharedPreferences().getString(getKey(), "0");
}
}
@Override
public void setText(String text) {
try {
if (getSharedPreferences() != null) {
getSharedPreferences().edit().putInt(getKey(), Integer.parseInt(text)).commit();
}
} catch (Exception e) {
// TODO: This catch stinks!
}
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
getEditText().setSelectAllOnFocus(true);
if (restoreValue) {
getEditText().setText(getText());
} else {
super.onSetInitialValue(restoreValue, defaultValue != null ? defaultValue : "");
}
}
}
请注意,可以将inputType
属性添加到EditTextPreference
android:inputType="number"
我没有走这条路的原因是我希望我的偏好被存储为一个Integer
而不是一个String
Kotlin + DataStore + androidx.Preference 的解决方法
免责声明!
可能这不是这样做的方法!
理想情况下,应该只:
- 将输入设置为 int
- 在类中覆盖
DataStore
:putInt
/getInt
扩展功能
就我而言,我有一个PreferenceFragmentCompat
,所以:
fun PreferenceFragmentCompat.setNumericInput(
@StringRes prefRes: Int, initialValue: String) {
val preference = findPreference(getString(prefRes)) as EditTextPreference?
preference?.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_SIGNED
// set the initial value: I read it from the DataStore and then
// pass it as the 2nd argument to setNumericInput.
// BTW, I do store stringPreferenceKeys, as it's the putString method
// that get's triggered
if (editText.text.isEmpty()) editText.setText(initialValue)
editText.setSelection(editText.text.length) // put cursor at the end
}
// to use it in the summary do something like:
preference?.setOnPreferenceChangeListener { it, newValue ->
it.summary = "updated: $newValue"
true
}
}
此外,在我扩展的活动中,BaseSettingsActivity
我使用以下方式替换管理SharedPreferences
:
preferenceManager.preferenceDataStore = dataStoreCvLogger