更改SearchView的光标颜色

7

我知道这个问题已经被提出了无数次,但是我无法为我的情况找到一个有效的解决方案。

我需要更改搜索视图(searchView)的光标颜色。我没有搜索视图的XML定义,我是在程序中使用它。

这是我的代码:

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;。谢谢。

2
如果您正在使用最新的AppCompat库,我认为您可以通过更改主题中的colorAccent值来更改光标颜色。 - David Heisnam
3个回答

39

你尝试过它吗?

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

<?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>

例如:如何在没有使用ActionBarSherlock的情况下更改SearchView中光标的颜色


1
工作正常但未能删除默认光标,因此两者同时显示。 - Brajendra Pandey
通过反射访问内部API不受支持,可能无法在所有设备上或未来正常工作。 - Kishan Solanki

4

改变光标颜色的另一种方法是通过样式实现。

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

 <?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>

在样式/主题资源文件夹中定义一个样式。
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>

请在相关的Activity样式或AppTheme中添加以下行:
 <item name="autoCompleteTextViewStyle">@style/cursorColor</item>

-1

只有将这段代码添加到onCreate()方法中才能让我正常工作

        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) {
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接