PIN entry widget for android
What is the best way of configuring a widget in android for entering a 4 digit PIN?
Currently I have:
<EditTextPreference
android:title="PIN"
android:summary="Your PIN number"
android:key="password"
android:numeric="integer"
android:maxLength="4"
android:password="true"
/>
This works quite well, but there is no way of constraining the input to be at least 4 digits. Is this how you would have someone set a PIN in preferences? Or is there a better way?
By the way, before people comment on security implications, I didn't choose the 4 digit PI开发者_如何学PythonN, it is for connecting to a third party system and I can't change it. And no, this isn't for banking.
You can add an OnPreferenceChangeListener and check the password length there.
E.g. if using a PreferenceActivity:
findPreference("password").setOnPreferenceChangeListener( new OnPreferenceChangeListener(){
public boolean onPreferenceChange (Preference preference, Object newValue){
if ((String)newValue).length == 4)
super.onPreferenceChange(preference, newValue);
else
Toast.makeText(this, "The PIN must have 4 digits", Toast.LENGTH_LONG).show();
}
});
There is an interesting View in Github that makes the coding of the 4 entry quite simple and easy to use. Check source
精彩评论