【问题标题】:Android edittext is underlined when typingAndroid edittext 输入时带下划线
【发布时间】:2016-02-10 18:27:19
【问题描述】:

在 Android 中输入编辑文本字段时,我需要删除下划线。对于名字,编辑文本的第一个字母应该是大写的,所以我给了textCapSentences,但在我的例子中,我在edittext字段中看到了下划线。如何删除它?

textFilter|textNoSuggestions|textCapSentences的所有组合我都试过了

这是我的名字编辑文本:

    <EditText
        android:id="@+id/signup_lname"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/signup_layer_bg2"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/signup_lname"
        android:imeOptions="actionDone"
        android:inputType="textFilter|textNoSuggestions|textCapSentences"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/custom_cursor" />

我的屏幕:

【问题讨论】:

标签: android android-edittext android-keypad android-inputtype


【解决方案1】:

android:inputType="textVisiblePassword" 禁用文本自动更正

【讨论】:

    【解决方案2】:

    其他答案对我都没有用。添加此下划线以在写作时建议单词。这是通过您的键盘应用程序(在我的例子中是 Gboard)上的设置启用的。

    所以你至少可以做三个选择:

    • 什么都不做,因为这是外部应用程序 (Gboard) 上的设置,并且是应该处理它的用户。

    • 要求用户禁用它并显示说明以解释如何执行此操作。这是:要从设置应用程序或打开的键盘导航到 gboard 设置并单击“设置”按钮,然后转到“文本/拼写更正”或类似内容并禁用第一个开关:显示建议条

      李>
    • 自行删除。由于 Gboard 将 UnderlineSpan 添加到 EditTexts 中的 EditableText 中,因此您只需首先查找即可将其删除。 Gboard 在 onTextChanged 和 afterTextChanged 之间的某个时间点添加了下划线,因此我必须在 afterTextChanged 方法上将其删除。

      editText.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           }
      
           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {
           }
      
           @Override
           public void afterTextChanged(Editable s) {
               for (UnderlineSpan span : s.getSpans(0, s.length(), UnderlineSpan.class)) {
                   s.removeSpan(span);
               }
      
               ...
           }
      });
      

    如果您需要保留任何其他 UnderlineSpan,您可以将此解决方案与此解决方案混合使用:https://stackoverflow.com/a/47704299/6552016

    【讨论】:

      【解决方案3】:

      结合你的答案,这对我来说真正有用:

      android:inputType="textVisiblePassword|textNoSuggestions"
      

      【讨论】:

      • 这在输入完成后有效。但是在输入时仍然显示下划线:(
      【解决方案4】:

      在每个单词下划线的是建议。试试这个:

      android:inputType="textNoSuggestions"
      

      【讨论】:

        【解决方案5】:

        使用这个:android:background="@android:color/transparent"

        【讨论】:

        • 如果您的编辑视图有下划线[即使 android:background="@null" 也可以],则此方法有效,但上述问题与建议有关。
        【解决方案6】:

        将其放入 .xml 文件中的编辑文本布局中

        android:background="@null"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-03
          • 1970-01-01
          • 1970-01-01
          • 2013-09-29
          • 2023-03-05
          • 2013-11-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多