Android EdittextPreference 提示 (android:hint)

发布于 2024-12-05 17:55:30 字数 271 浏览 1 评论 0原文

我有一个带有 EditTextPreference 的首选项屏幕。 代码中设置提示

如何在 xml 中

android:hint

setHint(int), setHint(CharSequence hint)

在 EditTextPreference 上(如 EditText 字段上)的 ? 我认为它就像在 EditText 上一样,但我没有找到类似的东西。 谢谢。

I have a preference screen with an EditTextPreference.
How to set a hint

either in xml like

android:hint

or in code like

setHint(int), setHint(CharSequence hint)

on the EditTextPreference like on an EditText field?
I assumed that it´s like on the EditText but i didn´t find anything like this.
Thanks.

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

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

发布评论

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

评论(6

等待我真够勒 2024-12-12 17:55:31

这更好,因为接受的答案不会在编辑文本上设置提示,而是将文本设置为
EditText(小差异万岁)。

要真正设置提示,请使用此代码:

YourEditTextPreference.getEditText().setHint(R.string.some_hint);

您还可以考虑添加摘要,以便首选项不会显示空摘要

this is way better, as the accepted answer will not set a hint on the edittext but will set the text to
the EditText (long live the small differene).

To really set a hint, use this code:

YourEditTextPreference.getEditText().setHint(R.string.some_hint);

you may also consider adding a summary so the preference will not be displayed with an empty summary

韵柒 2024-12-12 17:55:31

如果您使用的是 AndroidX 首选项库,则可以在 PreferenceFragmentCompat 中使用 OnBindEditTextListener 分配提示:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    PreferenceManager manager = getPreferenceManager();
    EditTextPreference textPref = manager.findPreference("myTextPreference");
    textPref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
        @Override
        public void onBindEditText(@NonNull EditText editText) {
            editText.setHint(R.string.hint);
        }
    });
}

当输入字段为空时,提示应按预期显示。

If you are using the AndroidX preference library, you can assign a hint using an OnBindEditTextListener in your PreferenceFragmentCompat:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    PreferenceManager manager = getPreferenceManager();
    EditTextPreference textPref = manager.findPreference("myTextPreference");
    textPref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
        @Override
        public void onBindEditText(@NonNull EditText editText) {
            editText.setHint(R.string.hint);
        }
    });
}

When the input field is empty, the hint should then be displayed as expected.

紫竹語嫣☆ 2024-12-12 17:55:31

Kotlin 版本(AndroidX 首选项库)

val editTextPref = findPreference<Preference>("prefKey") as EditTextPreference
editTextPref.setOnBindEditTextListener {
    it.hint = "My hint"
}

适用于 Android 10(API 级别 29)的模拟器。

同时,xml中指定的提示不起作用

<EditTextPreference
                android:key="prefKey"
                android:title="Title"
                android:hint="My hint"/>

Kotlin version (AndroidX preference library):

val editTextPref = findPreference<Preference>("prefKey") as EditTextPreference
editTextPref.setOnBindEditTextListener {
    it.hint = "My hint"
}

Works in emulator with Android 10 (API level 29).

At the same time, the hint specified in xml does not work:

<EditTextPreference
                android:key="prefKey"
                android:title="Title"
                android:hint="My hint"/>
静谧 2024-12-12 17:55:31

使用 android:summary简要描述您的偏好。

您还可以使用 setSummary(CharSequence 摘要)

编辑:为了给出“提示”,您可以使用android:defaultValue

Use android:summary to give a brief description of your preference.

You can also set this dynamically using setSummary(CharSequence summary) .

EDIT: for giving a 'hint' you could use android:defaultValue .

差↓一点笑了 2024-12-12 17:55:31

设置“提示”的方法是使用摘要提供程序,如中所述
https://developer.android.com/guide/topics/ ui/settings/customize-your-settings

想法是当值为 null 或空时打印默认/提示 - 否则,只需打印首选项值

val dobPreference: EditTextPreference? = findPreference("key_dob")

dobPreference?.summaryProvider = SummaryProvider<EditTextPreference> { pref ->

    if (pref.text.isNullOrBlank()) {
        "e.g. Enter your DOB in DDMMYYYY format" // the hint if pref is empty
    } else {
        pref.text // your preference value if it is not empty.
    }
}

the way to set "Hints" is to use a summary provider as described in
https://developer.android.com/guide/topics/ui/settings/customize-your-settings

the idea is to print a default/hint when the value is null or empty - otherwise, just print out the preference value

val dobPreference: EditTextPreference? = findPreference("key_dob")

dobPreference?.summaryProvider = SummaryProvider<EditTextPreference> { pref ->

    if (pref.text.isNullOrBlank()) {
        "e.g. Enter your DOB in DDMMYYYY format" // the hint if pref is empty
    } else {
        pref.text // your preference value if it is not empty.
    }
}
心舞飞扬 2024-12-12 17:55:30

在 XML 中为 EditTextPreference 设置提示与为 EditText 设置提示完全相同:

android:hint="This is my hint"

这会传递到对话框中的 EditText。

此行为在此处得到确认,其中显示“它是 DialogPreference 的子类,并在中显示 EditText”可以通过 getEditText() 以编程方式修改此 EditText,也可以通过在 EditTextPreference 上设置任何 EditText 属性来通过 XML 进行修改。

It's exactly the same to set a hint in XML for an EditTextPreference as it is for an EditText:

android:hint="This is my hint"

This gets passed through to the EditText in the dialog.

This behaviour is confirmed here where it says "It is a subclass of DialogPreference and shows the EditText in a dialog. This EditText can be modified either programmatically via getEditText(), or through XML by setting any EditText attributes on the EditTextPreference."

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