扩展AutoCompleteTextView让其默认显示一组列表。setThreshold
- /**
- * <p>Specifies the minimum number of characters the user has to type in the
- * edit box before the drop down list is shown.</p>
- *
- * <p>When <code>threshold</code> is less than or equals 0, a threshold of
- * 1 is applied.</p>
- *
- * @param threshold the number of characters to type before the drop down
- * is shown
- *
- * @see #getThreshold()
- *
- * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold
- */
这时我们可以创建一个类 继承AutoCompleteTextView,覆盖enoughToFilter,让其一直返回true就行。 然后再主动调用showDropDown方法, 就能在不输入任何字符的情况下显示下拉框。
- package com.wole.android.pad.view;
- import android.content.Context;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.widget.AutoCompleteTextView;
- /**
- * Created with IntelliJ IDEA.
- * User: denny
- * Date: 12-12-4
- * Time: 下午2:16
- * To change this template use File | Settings | File Templates.
- */
- public class InstantAutoComplete extends AutoCompleteTextView {
- private int myThreshold;
- public InstantAutoComplete(Context context) {
- super(context);
- }
- public InstantAutoComplete(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public InstantAutoComplete(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- public boolean enoughToFilter() {
- return true;
- }
- @Override
- protected void onFocusChanged(boolean focused, int direction,
- Rect previouslyFocusedRect) {
- super.onFocusChanged(focused, direction, previouslyFocusedRect);
- if (focused) {
- performFiltering(getText(), 0);
- showDropDown();
- }
- }
- public void setThreshold(int threshold) {
- if (threshold < 0) {
- threshold = 0;
- }
- myThreshold = threshold;
- }
- public int getThreshold() {
- return myThreshold;
- }
- }
- searchSuggestionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>(5));
- search_et.setAdapter(searchSuggestionAdapter);
- search_et.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- 没有输入任何东西 则显示默认列表,否则调用接口,展示下拉列表
- @Override
- public void afterTextChanged(Editable s) {
- if (s.length() >= 1) {
- if (fetchSearchSuggestionKeywordsAsyncTask != null) {
- fetchSearchSuggestionKeywordsAsyncTask.cancel(true);
- }
- fetchSearchSuggestionKeywordsAsyncTask =new FetchSearchSuggestionKeywordsAsyncTask();
- fetchSearchSuggestionKeywordsAsyncTask.execute();
- }else{
- showHotSearchKeywords();
- }
- }
- });
- search_et.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- String item = searchSuggestionAdapter.getItem(position);
- search_et.setText(item);
- search_btn.performClick();
- }
- });
- //点击autocompletetextview时,如果没有输入任何东西 则显示默认列表
- search_et.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (TextUtils.isEmpty(search_et.getText().toString())) {
- showHotSearchKeywords();
- }
- return false;
- }
- });
- //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果
- private void showHotSearchKeywords() {
- MiscUtil.prepareHotSearchKeywords(getWoleApplication());
- searchSuggestionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getWoleApplication().hotSearchHistoryKeywords);
- search_et.setAdapter(searchSuggestionAdapter);
- searchSuggestionAdapter.notifyDataSetChanged();
- search_et.showDropDown();
- }
- private class FetchSearchSuggestionKeywordsAsyncTask extends AsyncTask<Void, Void, List<String>> {
- @Override
- protected List<String> doInBackground(Void... params) {
- List<String> rt = new ArrayList<String>(5);
- String keyword = search_et.getText().toString();
- if (!TextUtils.isEmpty(keyword)) {
- try {
- String result = NetworkUtil.doGet(BaseActivity.this, String.format(Global.API_SEARCH_SUGGESTIOIN_KEYWORDS, URLEncoder.encode(keyword, "utf-8")), false);
- Log.i("FetchSearchSuggestionKeywordsAsyncTask", result);
- if (!TextUtils.isEmpty(result)) {
- JSONArray array = new JSONArray(result);
- for (int i = 0; i < array.length(); i++) {
- JSONObject jsonObject = array.getJSONObject(i);
- rt.add(jsonObject.optString("keyword"));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return rt;
- }
- @Override
- protected void onPostExecute(List<String> strings) {
- super.onPostExecute(strings);
- if (!strings.isEmpty()) {
- //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果
- searchSuggestionAdapter = new ArrayAdapter<String>(BaseActivity.this, android.R.layout.simple_dropdown_item_1line, strings);
- search_et.setAdapter(searchSuggestionAdapter);
- searchSuggestionAdapter.notifyDataSetChanged();
- }
- }
- }
扩展AutoCompleteTextView让其默认显示一组列表。setThreshold的更多相关文章
- DEDECMS点击主栏目默认显示第一个子栏目列表的方法
本文实例讲述了DEDECMS点击主栏目默认显示第一个子栏目列表的方法.分享给大家供大家参考.具体分析如下: 今天公司有个需求是,点击导航上的父栏目进去默认显示第一个子栏目的列表,以下是具体实现方法,可 ...
- 请问:关于织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表的问题
要设置织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表, 就按照如下图所示的方法进行操作,为什么 点击导航上的父栏目出现死循环呢, 根本浏览不了网页. 请各位大神指点指点,为什么点击 ...
- vue实现两重列表集合,点击显示,点击隐藏的折叠效果,(默认显示集合最新一条数据,点击展开,显示集合所有数据)
效果图: 默认显示最新一条数据: 点击显示所有数据: 代码: 说明:这里主要是 这块用来控制显示或者隐藏 根据当前点击的 这个方法里传递的index 对应 isShow 数组里的index ,对 ...
- Laravel大型项目系列教程(四)显示文章列表和用户修改文章
小编心语:不知不觉已经第四部分了,非常感谢很多人给小编提的意见,改了很多bug,希望以后能继续帮小编找找茬~小编也不希望误导大家~这一节,主要讲的 是如何显示文章列表和让用户修改文章,小编预告一下(一 ...
- centos中设置apache显示目录列表
apache中显示目录列表 在http.conf中加入如下代码(如有虚拟主机配置,加在虚拟主机配置段内),并把主目录内的index.pho,index.html,index.htm文件删除 复制代码 ...
- Android学习笔记:ListView简单应用--显示文字列表
在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...
- ionic 下拉选择框中默认显示传入的参数
开发过程当中遇到一个有趣的问题,如果我在第一个页面需要把 item { "ownerId" : 1 } 传递给第二个页面,并挂在$scope下 $scope.item = $sta ...
- /.nav-tabs :是普通标签页 .nav-pills:胶囊式标签页 action ;默认的激活项,给<li>加默认显示的是哪个标签页内容 .nav是标签页的一个基类,给ul加 .nav-stacked: 垂直排列BootStrap
<meta name="viewport" content="with=device-width, initial-scale=1, user-scalabe=no ...
- Django - 权限(4)- queryset、二级菜单的默认显示、动态显示按钮权限
一.queryset Queryset是django中构建的一种数据结构,ORM查询集往往是queryset数据类型,我们来进一步了解一下queryset的特点. 1.可切片 使用Python 的切片 ...
随机推荐
- Python数据类型方法
Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: ...
- python3.x Day5 面向对象
类:类是指:对具有相同属性的事物的抽象.蓝图.原型.在类中定义了这些事物都具备的属性和共同的方法. 对象:一个对象就是一个类实例化以后的实例,一个类必须经过实例化后才能在程序中被使用,一个类可以实例化 ...
- Adversarial Auto-Encoders
目录 Another Approach: q(z)->p(z) Intuitively comprehend KL(p|q) Minimize KL Divergence How to comp ...
- 利用类装饰器自定制property实现延迟计算
class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...
- javaWeb学习之 Filter过滤器----https://www.cnblogs.com/xdp-gacl/p/3948353.html
https://www.cnblogs.com/xdp-gacl/p/3948353.html
- CodeForcesGym 100517B Bubble Sort
Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...
- BNUOJ 1585 Girls and Boys
Girls and Boys Time Limit: 5000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ...
- hdu 3697 贪心
#include<stdio.h> #include<stdlib.h> #include<string.h> #define inf 0x3fffffff #de ...
- 2018/2/18 SpringCloud Eureka的学习和spirng ribbon的部分源码追踪
昨天对于Eurake大致做了一个介绍,今天就来说说具体怎么配置和使用吧. 首先,我们创建一个服务注册中心 这是它的配置文件 注意,因为我等下还会弄一个Eureka注册中心,所以这里service-ur ...
- Python基础之 一 补充
三元运算: 语法:result = 值1 if 条件 else 值2 当条件为真时,result = 值1 当条件为假时,result = 值2 进制: 二进制:01 八进制:01234567 十进制 ...