din*_*eth 492 android android-edittext android-styles
我有这个问题,我在平板电脑项目中使用Android的Holo主题.但是,我在屏幕上有一个具有白色背景的片段.我EditText在这个片段上添加了一个组件.我试图通过设置Holo.Light主题资源的背景来覆盖主题.但是,我的文本光标(克拉)仍然是白色的,因此在屏幕上看不见(我可以在编辑文本字段中隐约发现它...).
有谁知道如何让EditText使用更暗的光标颜色?我已经尝试将EditText的样式设置为"@android:style/Widget.Holo.Light.EditText"没有正面结果.
Dea*_*ean 1105
将android:textCursorDrawable属性设置为@null应导致使用android:textColor作为光标颜色.
属性"textCursorDrawable"在API级别12及更高级别中可用
sta*_*bit 443
在布局中
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/color_cursor"
/>
Run Code Online (Sandbox Code Playgroud)
然后创建drawable 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>
Run Code Online (Sandbox Code Playgroud)
EditText属性上有一个白色光标.
Zac*_*h-M 64
似乎所有的答案都围绕着灌木丛.
在你的EditText,使用属性:
android:textCursorDrawable="@drawable/black_cursor"
Run Code Online (Sandbox Code Playgroud)
并将drawable添加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>
Run Code Online (Sandbox Code Playgroud)
如果需要,这也是创建更多样化游标的方法.
R4j*_*R4j 47
在最新的Appcompactv21中有一种更改光标颜色的新方法
只需更改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>
Run Code Online (Sandbox Code Playgroud)
然后在您的应用主题或活动上应用此样式.
更新:这种方式仅适用于API 21+
Update 2:我不确定它可以工作的最小Android版本.
经Android版测试:
2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked
Run Code Online (Sandbox Code Playgroud)
din*_*eth 38
我找到了答案:)
我将Theme的editText样式设置为:
<item name="android:editTextStyle">@style/myEditText</item>
Run Code Online (Sandbox Code Playgroud)
然后我使用以下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>
Run Code Online (Sandbox Code Playgroud)
`
android:textCursorDrawable是这里的关键.
Jar*_*ler 24
对于需要EditText动态设置光标颜色的任何人,下面你会发现两种方法来实现这一点.
首先,创建你的光标drawable:
<?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>
Run Code Online (Sandbox Code Playgroud)
将光标drawable资源id设置为您创建的drawable(https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)) :
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}
Run Code Online (Sandbox Code Playgroud)
要仅更改默认光标drawable的颜色,可以使用以下方法:
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field fCursorDrawableRes =
TextView.class.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
Object editor = fEditor.get(editText);
Class<?> clazz = editor.getClass();
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
Drawable[] drawables = new Drawable[2];
Resources res = editText.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 ignored) {
}
}
Run Code Online (Sandbox Code Playgroud)
Cha*_*ruක 11
晚了,这是我的回答,
这适用于那些不想改变colorAccent父主题,但想要改变EditText属性的人!
这个答案演示如何改变......
- 底线颜色
- 光标颜色
- 光标指针颜色(我使用我的自定义图像)..........
EditText使用应用于Activity主题的样式.
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hey" />
Run Code Online (Sandbox Code Playgroud)
例:
<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>
Run Code Online (Sandbox Code Playgroud)
现在创建一个drawable(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>
Run Code Online (Sandbox Code Playgroud)
现在,您已在Activity主题中设置此样式.
示例:
在您的活动中,您有一个主题,将此自定义editText主题设置为该主题.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your Theme data -->
<item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
</style>
Run Code Online (Sandbox Code Playgroud)
小智 8
Edittext cursor color you want changes your color.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/color_cursor"
/>
Run Code Online (Sandbox Code Playgroud)
然后创建drawalble 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>
Run Code Online (Sandbox Code Playgroud)
小智 7
对我来说,我修改了AppTheme和值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>
Run Code Online (Sandbox Code Playgroud)
这是colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#B7EC2A</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我将android:textCursorDrawable属性取出到@null,我放在editText样式中.当我尝试使用它时,颜色不会改变.
小智 7
用这个
android:textCursorDrawable="@color/white"
Run Code Online (Sandbox Code Playgroud)
哇,我参加这个聚会真的很晚,但是它在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>
Run Code Online (Sandbox Code Playgroud)
您将在RES / COLOR文件夹中需要这一行代码
<color name="color_Black">#000000</color>
Run Code Online (Sandbox Code Playgroud)
为什么这么晚发布?考虑多种形式的类别可能会很不错,因为Android已经成为许多头怪兽!
我们可以在Meterial主题中做到这一点,如下所示:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="android:colorControlNormal">#ff0000</item>
<item name="android:colorControlActivated">#ff0000</item>
<item name="android:colorControlHighlight">#ff0000</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
如果您也想更改复选框和单选按钮的颜色,请添加以下行:
<item name="colorAccent">#ff0000</item>
Run Code Online (Sandbox Code Playgroud)
我已经在 Android API 21+ 中进行了测试
唯一有效的答案应该是更改活动的主题:
<item name="colorAccent">#000000</item>
您不应该使用android:textCursorDrawableto @null,因为这仅涉及光标本身,而不涉及光标下方的引脚(如果您想拖动光标)。主题解决方案是最严重的一种。
这里@Jared Rummler 的程序化 setCursorDrawableColor() 版本也适用于 Android 9 Pie。
@SuppressWarnings({"JavaReflectionMemberAccess", "deprecation"})
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
cursorDrawableResField.setAccessible(true);
int cursorDrawableRes = cursorDrawableResField.getInt(editText);
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editor = editorField.get(editText);
Class<?> clazz = editor.getClass();
Resources res = editText.getContext().getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Field drawableForCursorField = clazz.getDeclaredField("mDrawableForCursor");
drawableForCursorField.setAccessible(true);
Drawable drawable = res.getDrawable(cursorDrawableRes);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawableForCursorField.set(editor, drawable);
} else {
Field cursorDrawableField = clazz.getDeclaredField("mCursorDrawable");
cursorDrawableField.setAccessible(true);
Drawable[] drawables = new Drawable[2];
drawables[0] = res.getDrawable(cursorDrawableRes);
drawables[1] = res.getDrawable(cursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
cursorDrawableField.set(editor, drawables);
}
} catch (Throwable t) {
Log.w(TAG, t);
}
}
Run Code Online (Sandbox Code Playgroud)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item> -- change this one
</style>
Run Code Online (Sandbox Code Playgroud)
转到styles.xml 并更改颜色强调,这将影响编辑文本框中的光标
| 归档时间: |
|
| 查看次数: |
277669 次 |
| 最近记录: |