Android EdittextPreference 提示 (android:hint)
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这更好,因为接受的答案不会在编辑文本上设置提示,而是将文本设置为
EditText(小差异万岁)。
要真正设置提示,请使用此代码:
您还可以考虑添加摘要,以便首选项不会显示空摘要
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:
you may also consider adding a summary so the preference will not be displayed with an empty summary
如果您使用的是 AndroidX 首选项库,则可以在
PreferenceFragmentCompat
中使用OnBindEditTextListener
分配提示:当输入字段为空时,提示应按预期显示。
If you are using the AndroidX preference library, you can assign a hint using an
OnBindEditTextListener
in yourPreferenceFragmentCompat
:When the input field is empty, the hint should then be displayed as expected.
Kotlin 版本(AndroidX 首选项库):
适用于 Android 10(API 级别 29)的模拟器。
同时,xml中指定的提示不起作用:
Kotlin version (AndroidX preference library):
Works in emulator with Android 10 (API level 29).
At the same time, the hint specified in xml does not work:
使用
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
.设置“提示”的方法是使用摘要提供程序,如中所述
https://developer.android.com/guide/topics/ ui/settings/customize-your-settings 的
想法是当值为 null 或空时打印默认/提示 - 否则,只需打印首选项值
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
在 XML 中为 EditTextPreference 设置提示与为 EditText 设置提示完全相同:
这会传递到对话框中的 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:
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."