1. 程式人生 > >去除 ListView 在 setFilterText 設定過濾之後出現黑色彈框

去除 ListView 在 setFilterText 設定過濾之後出現黑色彈框

    在使用 ListView 實現好友通訊錄時,遇到問題:當給 ListView 的介面卡 BaseAdapter 實現 Filterable 介面之後,呼叫:

  mList.setFilterText(s.toString());

  方法之後,ListView會自動出現一個顯示當前過濾文字的黑色懸浮框,如圖:


    ListView 沒有暴露相關方法去除彈框,尋找發現 ListView 父類 AbsListView 中的相關控制元件:

    /**
     * Used with type filter window
     */
    EditText mTextFilter
;
    相關程式碼 AbsListView.java
    /**
     * Sets the initial value for the text filter.
     * @param filterText The text to use for the filter.
     *
     * @see #setTextFilterEnabled
     */
    public void setFilterText(String filterText) {
        // TODO: Should we check for acceptFilter()?
        if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) {
            createTextFilter(false);
            // This is going to call our listener onTextChanged, but we might not
            // be ready to bring up a window yet
            mTextFilter.setText(filterText);
            mTextFilter.setSelection(filterText.length());
            if (mAdapter instanceof Filterable) {
                // if mPopup is non-null, then onTextChanged will do the filtering
                if (mPopup == null) {
                    Filter f = ((Filterable) mAdapter).getFilter();
                    f.filter(filterText);
                }
                // Set filtered to true so we will display the filter window when our main
                // window is ready
                mFiltered = true;
                mDataSetObserver.clearSavedState();
            }
        }
    }
    相關程式碼 typing_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:textSize="36dp"
    android:textColor="#99FFFFFF"
    android:background="#BB000000"
    android:minWidth="240dip"
    android:maxWidth="240dip"
    android:padding="10dip"
    android:gravity="center_horizontal"
    android:focusable="false"
/>

解決方法,通過反射設定 mTextFilter 屬性達到隱藏彈框(或修改彈窗樣式效果):
    private void changeSearch(ListView listView) {
        try {
            Field field = listView.getClass().getSuperclass().getDeclaredField("mTextFilter");

            field.setAccessible(true);

            EditText searchAutoComplete = (EditText) field.get(listView);

//            searchAutoComplete.setTextColor(getResources().getColor(android.R.color.transparent));
//
//            searchAutoComplete.setBackgroundColor(getResources().getColor(android.R.color.transparent));

            searchAutoComplete.setVisibility(View.GONE);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
 
    設定之後,黑色彈框成功隱藏: