开发者

Number Preferences in Preference Activity in Android

开发者 https://www.devze.com 2023-01-06 00:43 出处:网络
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

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 ab开发者_Go百科le 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?


Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters.

EditTextPreference pref = (EditTextPreference)findPreference("preference_name");
pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);


You can also enforce it with the xml attribute android:numeric. The possible relevant values for this attribute are decimal and integer.


You can also do this directly in your preferences.xml. Something like this would work:

<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" />


If you are using a PreferenceActivity which you probably are, there is not one available.

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

}


In Android Jetpack Preference things changed, to access EditText you have to access like this

val preference = findPreference<EditTextPreference>(getString(R.string.pref_numdefault_key))
preference?.setOnBindEditTextListener {
    it.inputType = InputType.TYPE_CLASS_NUMBER
}
0

精彩评论

暂无评论...
验证码 换一张
取 消