搜索框输入文字显示搜索结果的效果

本文介绍如何通过AutoCompleteTextView在Android应用中实现实时搜索结果显示的功能。无需使用contentProvider,只需简单设置布局文件和Java代码,即可实现输入文字时下拉显示匹配结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有的时候我们在使用app的时候,在搜索框上输入开头几个字符就会弹出一个下拉框匹配出开头字符相符的搜索结果,这个效果是怎么做出来的呢。有的人用contentProvider做,而我采用的是一个比较偷懒的方法,用autoCompleteTextView做的。

布局文件中需要有这样一个控件

<AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:text="" >

        <requestFocus />
    </AutoCompleteTextView>

java文件中如下

<pre name="code" class="java">package com.example.autocomplexttextview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends ActionBarActivity {
	private AutoCompleteTextView atv;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		atv = (AutoCompleteTextView)this.findViewById(R.id.autoCompleteTextView1);
		adapter = new ArrayAdapter<String>(this,R.layout.a);
		adapter.add("东北大学");
		adapter.add("hello");
		atv.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


注意1:系统默认必须输入2个以上字符才开始进行搜索匹配,加入要实现输入一个字符就进行搜索,需要加上一句
atv<span style="white-space: pre;">.setThreshold(1);</span>

注意2:其中的

adapter = new ArrayAdapter<String>(this,R.layout.a);
这里本来采用的是android自带的layout,但是下拉框的背景颜色与字体颜色都是白色, 于是复制了官方源码,修改了下颜色

a.xml如下

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, 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.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
    android:singleLine="true"
    android:textColor="#ff000000"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值