【问题标题】:Change the color of the cursor of an EditText in Android across all the sdk在所有 sdk 中更改 Android 中 EditText 的光标颜色
【发布时间】:2012-07-16 10:23:47
【问题描述】:

我想在所有设备上更改 android 的 edittext 光标颜色。我该怎么做?

【问题讨论】:

    标签: android android-edittext text-cursor


    【解决方案1】:

    我不得不使用这样的drawable:

    mycursor.xml:

          <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
            <size android:width="1dp" />
            <solid android:color="@android:color/holo_blue_light"/> 
    <!--make sure its a solid tag not stroke or it wont work -->
        </shape>
    

    在我的编辑文本中,我将光标可绘制属性设置为:

                                <EditText
                                android:id="@+id/et_details"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:cursorVisible="true"
                               android:textCursorDrawable="@drawable/mycursor"  
                                />
    

    【讨论】:

      【解决方案2】:

      在所有平台上获取它的方法就是这样。

      1 - 在布局文件夹中打开 layout.xml。 找到编辑文本并设置

      android:cursorVisible="true"
      

      这将为低于 os 版本 11 的设备设置光标

      2 - 在可绘制文件夹中创建 cursor_drawable.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
          <size android:width="1dp" />
          <stroke android:color="@color/black"/>
      </shape>
      

      3 - 创建文件夹布局-v11

      4 - 将你的 layout.xml 复制到 layout-v11

      5 - 找到您的编辑文本并设置 android:textCursorDrawable="@drawable/cursor_drawable"

      这将使光标显示在所有设备和操作系统上。

      【讨论】:

      • 嗨,艾登,我也有同样的问题。我正在使用 2.2 api 构建开发应用程序。当我按照你说的做时,我在 android:textCursorDrawable 的 xml 文件中出现错误:textCursorDrawable has no reference ...请让我知道如何解决它....
      • 这也需要 Holo 主题,因此需要 Android 4.0。你读过那个吗?
      • 如果重力设置为正确并且提示设置为某个值,我无法使其正常工作。最初光标在提示前面,在您开始输入之前无法移动/向右对齐,然后提示消失并且光标向右对齐。
      • 看来你需要 来显示光标
      • 用实线替换笔画
      【解决方案3】:

      android:textCursorDrawable属性赋值给@null,并将android:textColor设置为光标颜色。

      【讨论】:

        【解决方案4】:

        创建drawalble xml:edtcolor_cursor

            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android" >
                <size android:width="1dp" />
                <solid android:color="#FFFFFF"  />
            </shape>
        
        <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:cursorVisible="true"
            android:textCursorDrawable="@drawable/edtcolor_cursor"
            />
        

        【讨论】:

          【解决方案5】:

          Refer 到此链接。 你可以试试这个

          android:textCursorDrawable

          【讨论】:

          • 我希望所有版本都这样。该属性仅在Android 3.0之后有效
          猜你喜欢
          • 1970-01-01
          • 2012-07-23
          • 1970-01-01
          • 2017-11-25
          • 1970-01-01
          • 2016-05-22
          • 1970-01-01
          • 2014-11-17
          • 2014-10-07
          相关资源
          最近更新 更多