Android 中 Preference Activity 中的数字偏好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3206765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number Preferences in Preference Activity in Android
提问by Kinglink
What I want to do is I am working on a game of life program. I want to take the time delay and make it a preference, but I want to make it available for people to type in a specific time. The number can be in miliseconds or seconds.
我想做的是我正在制作一个游戏人生节目。我想利用时间延迟并使其成为首选,但我想让人们可以在特定时间输入。该数字可以以毫秒或秒为单位。
However I'm a little stuck on how to proceed, I haven't been able to find a simple preference that already handles this, but there might be one. Is there an easy way to make this preference and confirm that the entered data is an integer or afloat?
但是,我对如何进行有些困惑,我无法找到一个已经可以处理此问题的简单偏好,但可能有一个。是否有一种简单的方法可以进行此首选项并确认输入的数据是整数还是浮点数?
采纳答案by jax
If you are using a PreferenceActivity which you probably are, there is not one available.
如果您正在使用可能使用的 PreferenceActivity,则没有可用的。
You will need to do something like this:
你需要做这样的事情:
/**
* Checks that a preference is a valid numerical value
*/
Preference.OnPreferenceChangeListener numberCheckListener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
//Check that the string is an integer.
return numberCheck(newValue);
}
};
private boolean numberCheck(Object newValue) {
if( !newValue.toString().equals("") && newValue.toString().matches("\d*") ) {
return true;
}
else {
Toast.makeText(ActivityUserPreferences.this, newValue+" "+getResources().getString(R.string.is_an_invalid_number), Toast.LENGTH_SHORT).show();
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get XML preferences
addPreferencesFromResource(R.xml.user_preferences);
//get a handle on preferences that require validation
delayPreference = getPreferenceScreen().findPreference("pref_delay");
//Validate numbers only
delayPreference.setOnPreferenceChangeListener(numberCheckListener);
}
回答by Kevin Westwood
Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters.
使用 EditTextPreference 并将输入类型设置为 TYPE_CLASS_NUMBER。这将强制用户输入数字而不是字母。
EditTextPreference pref = (EditTextPreference)findPreference("preference_name");
pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
回答by HRJ
You can also enforce it with the xml attribute android:numeric
. The possible relevant values for this attribute are decimal
and integer
.
您还可以使用 xml 属性强制执行它android:numeric
。此属性的可能相关值为decimal
和integer
。
回答by Jeshurun
You can also do this directly in your preferences.xml
. Something like this would work:
您也可以直接在您的preferences.xml
. 像这样的事情会起作用:
<EditTextPreference
android:defaultValue="100"
android:dialogTitle="@string/pref_query_limit"
android:inputType="number"
android:key="pref_query_limit"
android:summary="@string/pref_query_limit_summ"
android:title="@string/pref_query_limit" />
回答by dastan
In Android Jetpack Preference things changed, to access EditText you have to access like this
在 Android Jetpack Preference 中,事情发生了变化,要访问 EditText,您必须像这样访问
val preference = findPreference<EditTextPreference>(getString(R.string.pref_numdefault_key))
preference?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
}