【问题标题】:EditText's cursor horizontallyEditText 的光标水平
【发布时间】:2020-05-01 01:28:55
【问题描述】:

是否可以将 EditText 的光标水平放置而不是垂直放置?我想要如下图所示:

我读过一些主题是人们使用 android:textCursorDrawable,但我需要它来为 Android

编辑: 我正在添加 EditText 的 xml:

<EditText
    android:id="@+id/edittext1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
    android:ems="10"
    android:hint="@string/id"
    android:inputType="text"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:textCursorDrawable="@drawable/custom_bottom_cursor" />

【问题讨论】:

  • 使用 android:textCursorDrawable="@drawable/your_cursor_drawable"

标签: android android-edittext


【解决方案1】:

我正在寻找类似的东西,但我没有找到解决方案:(我发现的唯一一个想法是将 cursorVisible 设置为 false

editText.setCursorVisible(false);

然后动态追加字符_到edittext的末尾,设置闪烁动画到末尾

Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
lastChar.startAnimation(anim);

【讨论】:

  • 谢谢!如果我无法使 jyoon 的方法起作用,我将使用您的解决方法:)
  • 但问题是,@AlvaroSantisteban 是否能够使其工作? :D
  • 您的回答存在严重缺陷;如果你在有下划线_的时候输入,你输入的内容会出现在它后面,打断输入。
  • @jyoon 为什么?当用户键入某些内容时,我将添加 addTextChangedListener 以将 understore 移动到文件末尾...
  • @MatejSpili 对不起,我应该添加它:)
【解决方案2】:

1) 创建一个可绘制的光标。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size 
        android:width="20dp"
        android:height="1dp" />
    <solid
        android:color="#000000" />
    <padding 
        android:top="-20sp"
        android:bottom="1sp" />


</shape>

或者随你喜欢。
2) android:textCursorDrawable="@drawable/cursor_drawable"
但这仅适用于 Honeycomb 或更高版本...

【讨论】:

  • @iyoon 也许我做错了什么,但在新设备上我看不到光标,而在 android 2.3.6 的设备上我看到的光标和往常一样。我已将包含 EditText 的活动的布局放在文件夹 layout-v11 中,并将自定义可绘制对象放在 drawable-xhdpi 文件夹中。我还添加了 textCursorDrawable 和 cursorVisible。我做错了什么?
  • 自定义drawable应该放在drawable文件夹中,不指定dpi。
  • 还是没有运气。我更新了我的问题,以防万一:P
  • 我添加了 3.0 或更高版本的解决方案,我会寻找更低版本的答案。干杯。
  • 你真是太好了,如果你发现了什么,请告诉我:D
【解决方案3】:

这是我如何实现此屏幕的另一个示例:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="53dp"
        android:layout_height="67dp"
        android:inputType="numberDecimal"
        android:background="@drawable/background"
        android:textCursorDrawable="@drawable/line"
        android:cursorVisible="true"
        android:gravity="center"
        android:maxLength="1"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="2dp"
        android:color="#777777" />
    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

水平线

<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >
    <size android:width="20dp"/>
    <solid android:color="#9C27B0"/>
    <padding
        android:top="-47sp"
        android:bottom="-5sp"
        android:left="10dp"/>
</shape>

如果更改 EditText 的高度和宽度,则需要更新水平线的填充

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多