Android 在 EditText 中填充提示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10194187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:49:25  来源:igfitidea点击:

Padding for hint text inside EditText

androidandroid-edittextpadding

提问by Sandra

I want to be able to add padding to the text that is displayed inside the EditText, using

我希望能够为 EditText 内显示的文本添加填充,使用

android:hint="some text"

If I only add padding for the EditText regularly, it adds the padding for the whole view. And i want it for the text that gives a hint for the user, what needs to be entered. If someone knows how to do this, please give me a feedback.

如果我只定期为 EditText 添加填充,它会为整个视图添加填充。我希望它用于为用户提供提示的文本,需要输入的内容。如果有人知道如何做到这一点,请给我反馈。

enter image description here

在此处输入图片说明

回答by fanky10

You may find your solution in this answer: EditText set text start 10dp from left border

你可以在这个答案中找到你的解决方案: EditText set text start 10dp from left border

It helped me to achieve the padding for hint in username field:

它帮助我实现了用户名字段中提示的填充:

<EditText
 android:id="@+id/txtUsername"
 android:layout_width="match_parent"
 android:layout_height="30dp"
 android:layout_alignParentLeft="true"
 android:background="@drawable/bkg_login_txt_usuario"
 android:hint="@string/hint_username"
 android:paddingLeft="10dp"
 android:singleLine="true" >
 <requestFocus />
</EditText>

I hope this helps.

我希望这有帮助。

回答by jimmithy

Use the TextChangedListener and a TextWatcherto monitor the text inside the EditText. Use the TextWatcher check to see if the EditText is empty and set the padding, otherwise remove the padding.

使用 TextChangedListener 和TextWatcher来监视 EditText 中的文本。使用 TextWatcher 检查 EditText 是否为空并设置填充,否则删除填充。