how to remove close icon from edittext in searchview

扶醉桌前 提交于 2019-12-08 03:15:09

问题


I am trying to customize the searchview. i wanted to get rid of close icon in searchview. what i have done so far?

int closeId =android.support.v7.appcompat.R.id.search_close_btn;
ImageView close = (ImageView)mSearchView.findViewById(closeId);
close.setVisibility(View.GONE);

this code removes the close icon. But as soon as i start typing in editText it appears back in. How to permanently get rid of it?

close icon is remove when expanded searchView

it comes back in when i type in editText.


回答1:


Tried this?: Hide close button in android Search View

Change your theme like this:

For < v21:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarWidgetTheme">@style/AppTheme.WidgetTheme</item>
</style>

<style name="AppTheme.WidgetTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="searchViewCloseIcon">@android:color/transparent</item>
</style>

For v21 and above:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
    <item name="closeIcon">@android:color/transparent</item>
</style>

Also check this for more customization.




回答2:


In case you want do it programicaly, put a piece of code that hides "x" button in onQueryTextListener:

   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String query) {
            ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
            searchViewIcon.setVisibility(View.GONE);
            return true;
        }
   });


来源:https://stackoverflow.com/questions/36937046/how-to-remove-close-icon-from-edittext-in-searchview

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