«

Android自定义搜索框(EditText)的搜索功能实现,过滤ListView

时间:2024-3-2 20:04     作者:韩俊     分类: Android


实现自定义搜索框(实际上就是一个EditText):

<RelativeLayout
        android:id="@+id/music_search_rl"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_margin="5dp"
        android:background="@drawable/acm_inputbox"
        android:focusableInTouchMode="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableLeft="@drawable/icon_search"
            android:includeFontPadding="false"
            android:text="@string/search"
            android:textColor="#DFDFDF" />

        <EditText
            android:id="@+id/music_search_et"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:hint="@null"
            android:singleLine="true" />
    </RelativeLayout>

    <ListView
        android:id="@+id/search_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

acm_inputbox图片:

icon_search图片:

在相应的Activity类Java代码中,加入如下的功能代码:

//list.setTextFilterEnabled(true);

        search_et = (EditText)findViewById(R.id.music_search_et);
        search_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                String aa = s.toString();
                Pattern p = Pattern.compile(aa);

                //创建一个List集合,List集合的元素是Map
                List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
                for(int i = 0; i < pmiList.size(); i++) {
                    PhoneMusicInfo pmi = pmiList.get(i);
                    String pmiTitle = pmi.getTitle();
                    Matcher matcher = p.matcher(pmiTitle);

                    if(matcher.find()) {
                        Map<String, Object> listItem = new HashMap<String, Object>();
                        listItem.put("musicTitle", pmi.getTitle());
                        listItems.add(listItem);                        
                    }
                }

                //创建一个SimpleAdapter,设置新的Adapter,替换原来的Adapter
                SimpleAdapter simpleAdapter = new SimpleAdapter(SelectMusic.this, listItems, R.layout.music_item,
                        new String[] {"musicTitle"}, new int[] {R.id.music_item_tv});

                list.setAdapter(simpleAdapter);

                /*if(TextUtils.isEmpty(s))
                    list.clearTextFilter();
                else
                    list.setFilterText(s.toString());*/
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐