This is mostly a pet peeve of mine, but it annoys me to no end that the default behavior or EditTextPreference
s is to put the cursor at the beginning of the string. This ma开发者_如何学Gokes NO sense at all to me. In almost any other interface known to man (fine, ME), focusing on a textfield automatically sends the cursor to the END.
So. Is there an (easy) way to override this? I know I can extend from EditTextPreference
and call setSelection()
manually, but this seems like a really complicated solution for such a simple problem.
Another angle on this: I just wind up setting 'selectAllOnFocus' to true either in the XML definition, or programmatically on the object.
It basically accomplished what I wanted: to have the end user be able to replace the contents immediately.
Similar to lukeuser's solution, you can define the OnPreferenceClickListener
inline or as a class variable:
final OnPreferenceClickListener _moveCursorToEndClickListener =
new OnPreferenceClickListener()
{
@Override
public boolean onPreferenceClick(Preference preference)
{
EditTextPreference editPref = (EditTextPreference)preference;
editPref.getEditText().setSelection( editPref.getText().length() );
return true;
}
};
[...]
EditTextPreference myPref = (EditTextPreference)findPreference( "MyPref" );
myPref.setOnPreferenceClickListener( _moveCursorToEndClickListener );
Or
EditTextPreference myPref = (EditTextPreference)findPreference( "MyPref" );
myPref.setOnPreferenceClickListener(
new OnPreferenceClickListener()
{
@Override
public boolean onPreferenceClick(Preference preference)
{
EditTextPreference editPref = (EditTextPreference)preference;
editPref.getEditText().setSelection( editPref.getText().length() );
return true;
}
} );
I ended up extending EditTextPreference
and overriding the showDialog
method:
@Override
protected void showDialog(Bundle state) {
super.showDialog(state);
Handler delayedRun = new Handler();
delayedRun.post(new Runnable() {
@Override
public void run() {
EditText textBox = getEditText();
textBox.setSelection(textBox.getText().length());
}
});
}
I got another solution!
class opcl implements OnPreferenceClickListener{
@Override
public boolean onPreferenceClick(Preference pref) {
EditTextPreference et = (EditTextPreference) pref;
et.getEditText().setSelection(et.getText().length());
return true;
}
}
Then this for all your EditTextPreferences
opcl mopcl = new opcl();
etpref1.setOnPreferenceClickListener(mopcl);
etpref2.setOnPreferenceClickListener(mopcl);
etpref3.setOnPreferenceClickListener(mopcl);
This done the trick for me, just override onPreferenceTreeClick()
:
/**
* Automatically sets cursor to the last position in EditTextPreference
*
* @param preferenceScreen The {@link PreferenceScreen} that the
* preference is located in.
* @param preference The preference that was clicked.
* @return Whether the click was handled.
*/
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, @NotNull Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
Preference clickedPreference = findPreference (preference.getKey ());
if (clickedPreference instanceof EditTextPreference) // check if EditTextPreference
{
Editable editable = ((EditTextPreference) clickedPreference).getEditText().getText();
Selection.setSelection(editable, editable.length()) ; // set the cursor to last position
return true;
}
return false;
}
Keep in mind that I have used PreferenceFragment, but it is the same for PreferenceActivity.
This solution is adapted from http://www.phonesdevelopers.com/1816051/
精彩评论