Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

android - Changing the cursor color in SearchView without ActionBarSherlock

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...