您的位置:首页 > 其它

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

2015-05-16 20:01 375 查看
有的时候我们在使用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" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐