搜索框输入文字显示搜索结果的效果
2015-05-16 20:01
375 查看
有的时候我们在使用app的时候,在搜索框上输入开头几个字符就会弹出一个下拉框匹配出开头字符相符的搜索结果,这个效果是怎么做出来的呢。有的人用contentProvider做,而我采用的是一个比较偷懒的方法,用autoCompleteTextView做的。
布局文件中需要有这样一个控件
java文件中如下
注意1:系统默认必须输入2个以上字符才开始进行搜索匹配,加入要实现输入一个字符就进行搜索,需要加上一句
注意2:其中的
这里本来采用的是android自带的layout,但是下拉框的背景颜色与字体颜色都是白色, 于是复制了官方源码,修改了下颜色
a.xml如下
布局文件中需要有这样一个控件
<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" />
相关文章推荐
- 设计input搜索框提示文字点击消失的效果,提示文字与输入显示不同----->特别推荐
- ThinkPHP3.2.3---ajaxreturn的应用:一个在搜索框中输入,实时动态显示模糊搜索结果
- 利用Jquery实现,可输入搜索文字下拉选效果
- js中百度地图API获取本地搜索全部结果自定义显示效果
- 仿Android联系人SideBar排序,根据拼音A-Z字母快速导航联系人姓名,以及输入搜索条件过滤,显示姓名的文字图片,添加挤压动画
- 百度搜索结果页面的参数 提示词输入搜索框方式(rsv_sug5)
- js中百度地图API获取本地搜索全部结果自定义显示效果
- swift UI专项训练37 实时显示搜索结果的搜索框SearchBar
- 仿Android联系人SideBar排序,根据拼音A-Z字母快速导航联系人姓名,以及输入搜索条件过滤,显示姓名的文字图片
- 漂亮!Javascript代码模仿淘宝宝贝搜索结果的分页显示效果
- 百度搜索结果的URL参数 提示词输入搜索框方式(rsv_sug5)
- easyui-combobox---ajax获取数据库JSON数据,实现搜索框实时显示模糊搜索结果
- jquery实现微博文字输入框 输入时显示输入字数 效果实现
- jquery实现微博文字输入框 输入时显示输入字数 效果实现
- Axure7.0或更高版本中实现在搜索框中输入的文字在全局文字中显示特殊颜色
- 显示百度搜索页面下面的分页效果
- JQuery_ select2的搜索框不能输入搜索内容
- 百度搜索结果显示“我喜欢”按钮
- 高德地图输入提示,POI搜索并显示经纬度
- El capitan中Spotlight没有显示新特性(spotlight suggestion)的解决方法(天气,bing网页搜索结果,网络视频等)