Changing the cursor color in SearchView without ActionBarSherlock

旧巷老猫 提交于 2019-12-17 09:56:34

问题


I am attempting to change the color of the blinking cursor on the SearchView widget in ICS+. I have tried the following:

  • Adding <item name="android:textCursorDrawable">@null</item> to my theme
  • Adding a style for AutoCompleteTextViews to my theme and setting the textCursorAttribute of that style to @null
  • Setting android:textCursorDrawable="@null" directly on the SearchView

I read the answer here (Custom cursor color in SearchView), but there is not a non-ABS style for searchAutoCompleteTextView, so could not try this. I also looked for a Java method to set the text cursor drawable, but could not find one - I am modifying other aspects of the SearchView in Java and would be able to do so with the cursor if there were a method available.

I have customized the SearchView pretty extensively, but this one last change is keeping it from looking right - the cursor is white on a white background, so it is not easily visible. Any other ideas of things I can try?


回答1:


Based on the comments and answers above I made an example of how this could look using reflection. This solves the problem in my app. Hope it saves someone else some time.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.entity_list_actions, menu);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    final int textViewID = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null, null);
    final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(textViewID);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, 0); //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);
}

That 0 could be any other resource ID like R.drawable.my_cursor




回答2:


<style name="CustomTheme" parent="android:Theme.Holo.Light">
    ...
    <item name="android:autoCompleteTextViewStyle">@style/SearchViewStyle</item>
    ...
</style>

<style name="SearchViewStyle" parent="@android:style/Widget.Holo.Light.AutoCompleteTextView">
    <item name="android:textCursorDrawable">@drawable/action_mode_close</item>
</style>



回答3:


As per earlier comment:

If you dig into the Android source code, you'll find that mCursorDrawableRes only gets set once in the 3-param constructor. Unfortunately that means there is no easy way to change it at runtime. Looks like your options may be limited to using reflection, or moving your custom SearchView into the android.widget package in order to access the package protected member.




回答4:


This changes the text color and works also on Android 8:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete) searchView
                .findViewById(R.id.search_src_text);
        theTextArea.setTextColor(getResources().getColor(R.color.yourColor));
    ...


来源:https://stackoverflow.com/questions/18705185/changing-the-cursor-color-in-searchview-without-actionbarsherlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!