删除 SearchIcon 作为搜索视图中的提示 [英] Remove the SearchIcon as hint in the searchView

查看:18
本文介绍了删除 SearchIcon 作为搜索视图中的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个应用程序,例如当我点击图像(它是一个搜索视图)

I am doing an application where for example when i click on the image(it is a searchView)

搜索板打开!

它看起来像

但是这里显示了默认的搜索图标(放大镜),但是一旦输入了一些文本,它就会消失

but here the default search icon (magnifier) gets displayed but this dissappears as soon as some text is entered

但我不希望即使第一次点击图像也显示放大镜

but i dont want that magnifier to be displayed even the image is clicked for the first time

这里我没有使用任何 xml 文件

and here i am not using any xml file

我的代码是

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relative = new RelativeLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        relative.setLayoutParams(params);
        setContentView(relative);

        SearchView searchView = new SearchView(this);
        traverseView(searchView, 0);
//      searchView.setIconifiedByDefault(false);
        LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        searchView.setLayoutParams(searchViewparams);
        relative.addView(searchView);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private void traverseView(View view, int index) {
        if (view instanceof SearchView) {
            SearchView v = (SearchView) view;
            for(int i = 0; i < v.getChildCount(); i++) {
                traverseView(v.getChildAt(i), i);
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                traverseView(ll.getChildAt(i), i);
            }
        } else if (view instanceof EditText) {
            ((EditText) view).setTextColor(Color.GREEN);
            ((EditText) view).setHintTextColor(Color.BLACK);
        } else if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.BLUE);
        } else if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(R.drawable.ic_launcher);
        } else {
            Log.v("View Scout", "Undefined view type here...");
        }
    }
}

推荐答案

我也遇到了这个问题.我结合了我找到的教程和在 stackoverflow 中找到的现有答案.

I've had trouble with this too. I combined the tutorial I found and an existing answer found here in stackoverflow.

int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

注意searchView是一个LinearLayout,所以使用LinearLayout.LayoutParams来避免异常.

Take note that searchView is a LinearLayout, so use LinearLayout.LayoutParams to avoid an exception.

我也试过这个,但它没有删除视图.我似乎无法弄清楚为什么.:

I also tried this but it doesn't remove the view. I can't seem to figure why.:

magImage.setVisibility(View.GONE);

对于需要更改的其他视图,请参阅对此教程.

For the other views that you need to change, refer to this tutorial.

这篇关于删除 SearchIcon 作为搜索视图中的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆