有人知道怎么让EditText使用较暗的光标颜色吗?我尝试将EditText的样式设置为 "@android:style/Widget.Holo.Light.EditText",但没有积极的结果。
将android:textCursorDrawable
属性设置为@null
应该会使用android:textColor
作为光标颜色。
属性"textCursorDrawable"在API 12及以上版本可用。
target
版本号大于3.2,您可以使用它,并且它会被低版本忽略。 - Blundell在布局中
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/color_cursor"
/>
然后创建可绘制的XML:color_cursor。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>
你的EditText属性上有一个白色光标。
@null
会使其变得非常细。 - Rahul Gaur看起来所有的答案都绕着弯子说。
在你的EditText
中,使用属性:
android:textCursorDrawable="@drawable/black_cursor"
并将可绘制项black_cursor.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="#000000"/>
</shape>
如果需要创建更多样化的光标,这也是一种方法。
Appcompact
v21中,有一种新的改变光标颜色的方法。只需像这样在样式中更改colorAccent
即可: <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#088FC9</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#088FC9</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<!-- THIS IS WHAT YOU'RE LOOKING FOR -->
<item name="colorAccent">#0091BC</item>
</style>
然后在您的应用程序主题或活动上应用此样式。
更新:此方法仅适用于API 21+
更新2:我不确定它可以工作的最低Android版本。
已通过以下Android版本进行测试:
2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked
colorAccent
更改例如 EditText
底部的线条,但它不会触及实际的光标颜色。哦,当然不再有“v21”了。 - Neph我找到了答案 :)
我将主题的editText样式设置为:
<item name="android:editTextStyle">@style/myEditText</item>
然后我使用了以下drawable来设置光标:`<style name="myEditText" parent="@android:style/Widget.Holo.Light.EditText">
<item name="android:background">@android:drawable/editbox_background_normal</item>
<item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item>
<item name="android:height">40sp</item>
</style>
android:textCursorDrawable在这里起着关键作用。
android:editTextStyle
时遇到了问题,但将其更改为editTextStyle
后问题得以解决。请参见https://dev59.com/VVwY5IYBdhLWcg3wpJL_#34010235。 - shoe对于任何需要动态设置EditText
光标颜色的人,以下将介绍两种实现方法。
首先,创建您的光标可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
<size android:width="1dp" />
</shape>
将光标可绘制资源ID设置为您创建的可绘制对象(source)):
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}
要只改变默认光标可绘制的颜色,您可以使用以下方法:
public static void setTextCursorColor(TextView textView, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
final Drawable drawable = textView.getTextCursorDrawable();
drawable.setTint(color);
textView.setTextCursorDrawable(drawable);
} else try {
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
int mCursorDrawableRes = fCursorDrawableRes.getInt(textView);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
Object editor = fEditor.get(textView);
Class<?> clazz = editor.getClass();
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
Drawable[] drawables = new Drawable[2];
Resources res = textView.getContext().getResources();
drawables[0] = res.getDrawable(mCursorDrawableRes);
drawables[1] = res.getDrawable(mCursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
fCursorDrawable.set(editor, drawables);
} catch (final Throwable throwable) {
if (DEBUG) throw new RuntimeException("can't set text cursor color", throwable);
}
}
AppCompatEditText
已经有了setSupportBackgroundTintList
,所以是否很容易添加类似setSupportCursorTintList
的东西呢? - Joseph Piché晚来一步,以下是我的答案:
这篇文章适用于那些不想更改父主题中的colorAccent
,但希望更改EditText
属性的人!
本回答演示了如何使用应用于活动主题的样式更改......
- 底线颜色
- 光标颜色
- 光标指针颜色(我使用了自定义图片)......
EditText 的。
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hey" />
示例:
<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
<item name="android:textColor">@color/white</item>
<item name="android:textColorHint">#8AFFFFFF</item>
<item name="android:background">@drawable/edit_text_background</item> // background (bottom line at this case)
<item name="android:textCursorDrawable">@color/white</item> // Cursor
<item name="android:textSelectHandle">@drawable/my_white_icon</item> // For pointer normal state and copy text state
<item name="android:textSelectHandleLeft">@drawable/my_white_icon</item>
<item name="android:textSelectHandleRight">@drawable/my_white_icon</item>
</style>
现在创建一个可绘制对象(edit_text_background
),添加用于背景的资源 xml!您可以根据自己的需要进行自定义!
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/white"/>
</shape>
</item>
</layer-list>
现在,您已在Activity主题中设置了这个样式。
示例:
在您的Activity中,您有一个主题,请将此自定义editText
主题设置为该主题。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your Theme data -->
<item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
</style>
<item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
没有效果。 - act262<item name="android:textCursorDrawable">@color/white</item>
不起作用。 - Vlad哇,我来晚了,但是这个问题在17天前有活动。看起来我们需要考虑发布我们正在使用的Android版本的答案,因此目前这个答案适用于Android 2.1及以上版本。请前往RES/VALUES/STYLES并添加以下代码行,您的光标将变为黑色。
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!--<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">-->
<!-- Customize your theme here. -->
<item name="colorControlActivated">@color/color_Black</item>
<!--Sets COLOR for the Cursor in EditText -->
</style>
<color name="color_Black">#000000</color>
为什么晚发帖?考虑到安卓已经成为了一个具有许多头的怪物,可能需要考虑一些分类形式!
Edittext cursor color you want changes your color.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/color_cursor"
/>
color_cursor
。<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>
对于我来说,我修改了AppTheme和values/colors.xml两个值。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorControlNormal">@color/yellow</item>
<item name="colorAccent">@color/yellow</item>
</style>
以下是colors.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#B7EC2A</color>
</resources>
我将放置在editText样式中的android:textCursorDrawable属性移除为@null。当我尝试使用此功能时,颜色没有改变。