首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >改变searchView的光标颜色

改变searchView的光标颜色
EN

Stack Overflow用户
提问于 2015-08-29 15:57:57
回答 3查看 9.1K关注 0票数 7

我知道这个问题已经发布了上百万次了,但我无法为我的案子找到一个可行的解决方案。

我需要更改我的searchView的光标颜色。我没有searchView的xml定义,而是以编程方式使用它。

这里我的代码:

代码语言:javascript
运行
复制
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_contacts, menu);

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    return true;
}

我用的是:import android.support.v7.widget.SearchView;

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-29 16:27:04

你试过了吗?

代码语言:javascript
运行
复制
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_contacts, menu);

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    AutoCompleteTextView searchTextView = (AutoCompleteTextView) search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {
    }
    return super.onCreateOptionsMenu(menu);
}

drawable/cursor.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <size android:width="2dp" />
</shape>

例如:Changing the cursor color in SearchView without ActionBarSherlock

票数 39
EN

Stack Overflow用户

发布于 2021-06-26 16:35:36

另一种改变光标颜色的方法可以通过样式来完成。

首先创建cursor.xml可绘制文件

代码语言:javascript
运行
复制
 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />   //your desired cursor color
<size android:width="2dp" />
</shape>

在“样式/主题”文件夹中定义样式

代码语言:javascript
运行
复制
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>

并在相关的活动样式或AppTheme中添加下面一行

代码语言:javascript
运行
复制
 <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
票数 3
EN

Stack Overflow用户

发布于 2020-03-29 09:56:43

只有在onCreate()方法中添加此代码才适用于我

代码语言:javascript
运行
复制
        searchView = (SearchView)findViewById(R.id.search_view);
        SearchView.SearchAutoComplete searchAutoComplete =
                (SearchView.SearchAutoComplete)searchView.findViewById(androidx.appcompat.R.id.search_src_text);
        AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
        try {
            Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
            mCursorDrawableRes.setAccessible(true);
            mCursorDrawableRes.set(searchTextView, 0);
        } catch (Exception e) {
        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32287938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档