EditTextPreference 和默认光标位置

发布于 2024-11-05 17:30:57 字数 269 浏览 1 评论 0原文

这主要是我的一个小烦恼,但它让我烦恼不已,因为默认行为或 EditTextPreference 是将光标放在字符串的开头。这对我来说毫无意义。在几乎所有其他人类已知的界面中(很好,ME),聚焦于文本字段会自动将光标发送到 END。

所以。有没有一种(简单)方法可以覆盖这个?我知道我可以从 EditTextPreference 扩展并手动调用 setSelection(),但这对于如此简单的问题来说似乎是一个非常复杂的解决方案。

This is mostly a pet peeve of mine, but it annoys me to no end that the default behavior or EditTextPreferences is to put the cursor at the beginning of the string. This makes 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疧_╮線 2024-11-12 17:30:57

另一个角度是:我只是在 XML 定义中或以编程方式在对象上将“selectAllOnFocus”设置为 true。

它基本上完成了我想要的:让最终用户能够立即替换内容。

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.

挽袖吟 2024-11-12 17:30:57

与 lukeuser 的解决方案类似,您可以内联定义 OnPreferenceClickListener 或将其定义为类变量:

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

或者

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

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;
        }
    } );
月下客 2024-11-12 17:30:57

我最终扩展了 EditTextPreference 并覆盖了 showDialog 方法:

  @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 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());
      }
    });
  }
红墙和绿瓦 2024-11-12 17:30:57

我有另一个解决方案!

class opcl implements OnPreferenceClickListener{
    @Override
    public boolean onPreferenceClick(Preference pref) {
        EditTextPreference et = (EditTextPreference) pref;
        et.getEditText().setSelection(et.getText().length());
        return true;
    }
}

然后这适用于您所有的 EditTextPreferences

opcl mopcl = new opcl();
    etpref1.setOnPreferenceClickListener(mopcl);
    etpref2.setOnPreferenceClickListener(mopcl);
    etpref3.setOnPreferenceClickListener(mopcl);

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);
海之角 2024-11-12 17:30:57

这对我来说已经成功了,只需重写 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;
}

请记住,我已经使用了 PreferenceFragment,但它对于 PreferenceActivity 是相同的。

该解决方案改编自 http://www.phonesdevelopers.com/1816051/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文