如何从 Android 中的 EditText 字段中删除下划线?
每当在 EditText 框中键入单词时,我总是会在键入的单词下方看到一条下划线。但是当我在该单词后按空格时,我不再看到下划线。
我的要求是在用户键入消息时删除该下划线。
添加了屏幕截图,我们看到史密斯带有下划线。但我不希望这种事发生。
下面是我用于 AlertDialog 框的 xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="@string/alert_dialog_name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:inputType="textPersonName"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Whenever a word is typed in the EditText box, I always see an underline under the word being typed. But when I press a space after that word I no longer see the underline.
My reqirement is to remove that underline when the user is typing the message.
Added is the screenshot and we see that Smith is underlined. But I don't want this to happen.
Below is the xml that I use for the AlertDialog box.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="@string/alert_dialog_name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:inputType="textPersonName"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
android:inputType="textNoSuggestions"
android:inputType="textNoSuggestions"
有一个函数可以删除任何组合状态。我认为如果您在每次用户键入后调用它,您将看不到下划线。我在用户完成输入后使用它来获取整个 textView 的绘图缓存(我需要没有下划线)。它是
There is a function that removes any composing state. I think if you call it after every time a user types, you will not see the underline. I use it after the user finishes typing, to get the drawing cache of the entire textView (which I need without underline). It's
您不必使用任何逻辑来删除下划线——只需在想要使用该值时调用 getText().toString() 即可。它不会包含特殊格式或任何内容。
You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything.
接受的答案没有给出解决方案,其他一些用户给出了答案,但没有人给出完整的答案,所以这就是我在这里写下工作解决方案的原因。
如果你想删除下划线,只需添加以下代码即可。
那么以上信息可能对其他人有帮助。
accepted answer is not given solution, and some other user given answer but no one given full anser, so that's why i write here working solution.
If you want to remove underline then just add below code.
May be above information is helpful to others.
最好的解决方案是在
EditText
上添加:The best solution for this is to add on your
EditText
:如果 EditText 视图是动态的(从类文件创建),请从您的类中使用它:
或者在 XML 中将
android:inputType="textNoSuggestions"
包含在您的 EditText 中。Use this from your class, if EditText view is dynamic (created from class file):
OR include
android:inputType="textNoSuggestions"
with your EditText in XML.如果您想保留表情符号并且
textNoSuggestions
不适合您,请阅读本文。textNoSuggestions
并非在所有键盘上都适用。inputType="textVisiblePassword"
可以工作,但它会从大多数键盘中删除表情符号。我发现设置
inputType="textUri"
有效并保留添加表情符号的功能。Read this if you want to keep emojis and
textNoSuggestions
doesn't work for you.textNoSuggestions
does not work in every keyboard.inputType="textVisiblePassword"
works but it removes emoji's from most keyboards.I found that setting
inputType="textUri"
works and keeps the ability to add emojis.尝试:
try: