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

实现自定义搜索框(实际上就是一个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
				
			}
		});

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值