【问题标题】:EditText: How to show cursor and hide underline at a time?EditText:如何一次显示光标和隐藏下划线?
【发布时间】:2018-09-02 20:35:57
【问题描述】:

我正在开发一个计算器应用程序。我想像 Android 默认计算器应用程序一样实现 UI。请查看我的应用截图。

我想隐藏EditText 的下划线并以white 颜色显示光标。我在 EditText 使用透明背景(来自here)也使用背景作为@null。它隐藏了 EditText 的下划线并隐藏了光标。但是...对于计算器应用程序光标不应隐藏。

请给我一种方法来隐藏EditText 的下划线并显示EditText 的白色光标。

【问题讨论】:

  • edittext.setBackground(null)

标签: android android-edittext


【解决方案1】:

将可绘制的背景设置为透明,并将 android:textCursorDrawable 设置为空,以便光标颜色始终为文本颜色。例如:

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

【讨论】:

    【解决方案2】:

    您可以通过程序将EditText 背景过滤器设置为此。这会将其设置为清晰的背景颜色。

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

    您也可以通过 XML 执行此操作,但这仅适用于 Android API 21 +,您最好的选择是颜色过滤器。

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

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

    【讨论】:

      【解决方案3】:

      请在您的EditText 中再添加一件事:=

      <EditText
                      android:id="@+id/youId"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="#00000000"
                      />
      

      【讨论】:

        【解决方案4】:

        将背景可绘制设置为透明并设置您选择的文本光标可绘制。例如:

        <EditText
                        android:id="@+id/remarkEditText"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:textCursorDrawable="@drawable/white"/>
        

        【讨论】:

        • 谢谢先生。有用!我刚刚创建了一个可绘制的自定义光标,例如:&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;size android:width="1dp" android:height="5dp"/&gt; &lt;solid android:color="#FFFFFF"/&gt;
        猜你喜欢
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 2014-12-13
        • 1970-01-01
        • 1970-01-01
        • 2021-09-16
        • 2017-03-10
        • 1970-01-01
        相关资源
        最近更新 更多