在所有 sdk 中更改 Android 中 EditText 的光标颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11523494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of the cursor of an EditText in Android across all the sdk
提问by Kishore
Want to change the android's edittext cursor color, that has to be worked on across all the devices
想要更改 android 的 edittext 光标颜色,必须在所有设备上进行处理
回答by j2emanue
i had to use a drawable like this:
我不得不使用这样的drawable:
mycursor.xml:
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>
in my edit text i set the cursor drawable attributes like this:
在我的编辑文本中,我设置了这样的光标可绘制属性:
<EditText
android:id="@+id/et_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="@drawable/mycursor"
/>
回答by Aiden Fry
The way to get it across all platforms is as such.
在所有平台上获得它的方法就是这样。
1 - Open your layout.xml in your layout folder. Find the edit text and set
1 - 在您的布局文件夹中打开您的 layout.xml。找到编辑文本并设置
android:cursorVisible="true"
this will set the cursor for devices lower that os version 11
这将为低于操作系统版本 11 的设备设置光标
2 - Create your cursor_drawable.xml in the drawable folder
2 - 在 drawable 文件夹中创建 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 - Create a folder layout-v11
3 - 创建文件夹 layout-v11
4 - Copy your layout.xml into the layout-v11
4 - 将您的 layout.xml 复制到 layout-v11
5 - Find your edit text and set android:textCursorDrawable="@drawable/cursor_drawable"
5 - 找到您的编辑文本并设置 android:textCursorDrawable="@drawable/cursor_drawable"
This will make a cursor be shown on all devices and OS.
这将使光标显示在所有设备和操作系统上。
回答by AkashG
Assign android:textCursorDrawable
attribute to @null
and set android:textColor
as the cursor color.
将android:textCursorDrawable
属性分配给@null
并设置android:textColor
为光标颜色。
回答by Makvin
Create drawalble xml: edtcolor_cursor
创建 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"
/>