当它是空的android时,如何在searchview中隐藏关闭按钮

时间:2016-09-23 10:36:39

标签: android searchview

我正在开发一个Android应用程序,并在其中实现了searchview。我想在searchview初始化时隐藏关闭按钮,并在我开始输入时仅显示该按钮。当searchview为空时,隐藏它的关闭按钮。

我已经完成了以下工作。

public void initializeSearchView(){

    SearchManager searchManager = (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(((Activity)mContext).getComponentName()));
    mSearchView.setIconifiedByDefault(true);
    mSearchView.setIconified(false);
    mSearchView.clearFocus();
    int searchImgId = mContext.getResources().getIdentifier("android:id/search_mag_icon", null, null);
    ImageView searchIconView = (ImageView) mSearchView.findViewById(searchImgId);
    if(searchIconView != null){
        searchIconView.setImageResource(R.drawable.ic_action_search);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        searchIconView.setLayoutParams(layoutParams);
    }
    int searchPlateId = mSearchView.getContext().getResources().
            getIdentifier("android:id/search_plate", null, null);
    View searchPlate = mSearchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        int searchTextId = searchPlate.getContext().getResources().
                getIdentifier("android:id/search_src_text", null, null);
        final TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        searchText.setHintTextColor(ContextCompat.getColor(mContext, R.color.gray_stroke_color));
        mSearchView.setQueryHint("Search");
        int searchCloseBtnId = searchPlate.getContext().getResources().
                getIdentifier("android:id/search_close_btn", null, null);
        searchCloseBtn = (ImageView) searchPlate.findViewById(searchCloseBtnId);
        searchCloseBtn.setVisibility(View.GONE);
        searchCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchText.setText("");
                mSearchView.clearFocus();
            }
        });
    }
    mSearchView.setOnQueryTextListener(this);
}

我在搜索视图初始化时将Visibility GONE设置为关闭按钮,但它无法正常工作。我怎样才能做到这一点,如果有人在这里有想法,请帮助。

非常感谢先进。

3 个答案:

答案 0 :(得分:0)

searchCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //searchText.setText("");
                //mSearchView.clearFocus();
                mSearchView.setIconified(false);
            }
        }); 

答案 1 :(得分:0)

首先,Gone参数应该有效。也许你拿错了View,试着用简单的:

 ImageView closeButton =
 (ImageView)mSearchView.findViewById(R.id.search_close_btn);

您也可以尝试为View设置LayoutParams:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 0);
closeBut.setLayoutParams(layoutParams);

答案 2 :(得分:0)

尝试一下技巧:

if (searchCloseBtn != null) {
    searchCloseBtn.setEnabled(false);
    searchCloseBtn.setImageDrawable(getResources().getDrawable(R.drawable.transparent));
}

因为当用户与SearchView交互时,SearchView会重置其关闭按钮的可见性。