EditText: 如何同时显示光标和隐藏下划线?

4
我正在开发一个计算器应用程序。我想要实现与Android默认计算器应用程序相似的用户界面。请查看我的应用程序截图。

enter image description here

我想隐藏EditText的下划线并将光标显示为白色。我在EditText中使用了透明背景(来自这里),同时将背景设置为@null。它隐藏了EditText的下划线,但也隐藏了光标。但是...对于计算器应用程序,不应该隐藏光标。
请告诉我如何隐藏EditText的下划线并显示白色的EditText光标。

edittext.setBackground(null) - ADM
4个回答

3
将背景可绘制设置为透明,并将文本光标可绘制设置为您选择的样式。例如:
<EditText
                android:id="@+id/remarkEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:textCursorDrawable="@drawable/white"/>

1
谢谢您先生。它有效了! 我刚刚创建了一个自定义光标可绘制对象,如下所示:<shape xmlns:android="http://schemas.android.com/apk/res/android"> <size android:width="1dp" android:height="5dp"/> <solid android:color="#FFFFFF"/> </shape>` - Hasan Abdullah

2

将背景drawable设置为透明,并将android:textCursorDrawable设置为null,以便光标颜色始终为文本颜色。例如:

<EditText
    android:id="@+id/editTextInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:textCursorDrawable="@null"/>

0
请在您的EditText中添加一件事:=
<EditText
                android:id="@+id/youId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                />

0

通过编程,您可以将EditText的背景过滤器设置为此值。这将使其背景颜色变为透明。

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

或者你可以通过 XML 实现这个功能,但是只能在 Android API 21 及以上版本中使用,最好的选择还是颜色过滤器。

android:backgroundTint="@android:color/transparent"

对于光标,您应该通过XML添加textCursorDrawablecursorVisible


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接