很多时候, 在做自动下拉框时,默认点上去时需要显示一组默认的下拉数据。但是默认的AutoCompleteTextView是实现不了的, 因为setThreshold方法最小值是1,就算你设的值为0,也会自动改成1的。

  1. /**
  2. * <p>Specifies the minimum number of characters the user has to type in the
  3. * edit box before the drop down list is shown.</p>
  4. *
  5. * <p>When <code>threshold</code> is less than or equals 0, a threshold of
  6. * 1 is applied.</p>
  7. *
  8. * @param threshold the number of characters to type before the drop down
  9. *                  is shown
  10. *
  11. * @see #getThreshold()
  12. *
  13. * @attr ref android.R.styleable#AutoCompleteTextView_completionThreshold
  14. */

这时我们可以创建一个类 继承AutoCompleteTextView,覆盖enoughToFilter,让其一直返回true就行。 然后再主动调用showDropDown方法, 就能在不输入任何字符的情况下显示下拉框。

  1. package com.wole.android.pad.view;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.util.AttributeSet;
  5. import android.widget.AutoCompleteTextView;
  6. /**
  7. * Created with IntelliJ IDEA.
  8. * User: denny
  9. * Date: 12-12-4
  10. * Time: 下午2:16
  11. * To change this template use File | Settings | File Templates.
  12. */
  13. public class InstantAutoComplete extends AutoCompleteTextView {
  14. private int myThreshold;
  15. public InstantAutoComplete(Context context) {
  16. super(context);
  17. }
  18. public InstantAutoComplete(Context context, AttributeSet attrs) {
  19. super(context, attrs);
  20. }
  21. public InstantAutoComplete(Context context, AttributeSet attrs, int defStyle) {
  22. super(context, attrs, defStyle);
  23. }
  24. @Override
  25. public boolean enoughToFilter() {
  26. return true;
  27. }
  28. @Override
  29. protected void onFocusChanged(boolean focused, int direction,
  30. Rect previouslyFocusedRect) {
  31. super.onFocusChanged(focused, direction, previouslyFocusedRect);
  32. if (focused) {
  33. performFiltering(getText(), 0);
  34. showDropDown();
  35. }
  36. }
  37. public void setThreshold(int threshold) {
  38. if (threshold < 0) {
  39. threshold = 0;
  40. }
  41. myThreshold = threshold;
  42. }
  43. public int getThreshold() {
  44. return myThreshold;
  45. }
  46. }
  1. searchSuggestionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>(5));
  2. search_et.setAdapter(searchSuggestionAdapter);
  3. search_et.addTextChangedListener(new TextWatcher() {
  4. @Override
  5. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  6. }
  7. @Override
  8. public void onTextChanged(CharSequence s, int start, int before, int count) {
  9. }
  10. 没有输入任何东西 则显示默认列表,否则调用接口,展示下拉列表
  11. @Override
  12. public void afterTextChanged(Editable s) {
  13. if (s.length() >= 1) {
  14. if (fetchSearchSuggestionKeywordsAsyncTask != null) {
  15. fetchSearchSuggestionKeywordsAsyncTask.cancel(true);
  16. }
  17. fetchSearchSuggestionKeywordsAsyncTask =new FetchSearchSuggestionKeywordsAsyncTask();
  18. fetchSearchSuggestionKeywordsAsyncTask.execute();
  19. }else{
  20. showHotSearchKeywords();
  21. }
  22. }
  23. });
  24. search_et.setOnItemClickListener(new OnItemClickListener() {
  25. @Override
  26. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  27. String item = searchSuggestionAdapter.getItem(position);
  28. search_et.setText(item);
  29. search_btn.performClick();
  30. }
  31. });
  32. //点击autocompletetextview时,如果没有输入任何东西 则显示默认列表
  33. search_et.setOnTouchListener(new View.OnTouchListener() {
  34. @Override
  35. public boolean onTouch(View v, MotionEvent event) {
  36. if (TextUtils.isEmpty(search_et.getText().toString())) {
  37. showHotSearchKeywords();
  38. }
  39. return false;
  40. }
  41. });
  1. //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果
  2. private void showHotSearchKeywords() {
  3. MiscUtil.prepareHotSearchKeywords(getWoleApplication());
  4. searchSuggestionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getWoleApplication().hotSearchHistoryKeywords);
  5. search_et.setAdapter(searchSuggestionAdapter);
  6. searchSuggestionAdapter.notifyDataSetChanged();
  7. search_et.showDropDown();
  8. }
  9. private class FetchSearchSuggestionKeywordsAsyncTask extends AsyncTask<Void, Void, List<String>> {
  10. @Override
  11. protected List<String> doInBackground(Void... params) {
  12. List<String> rt = new ArrayList<String>(5);
  13. String keyword = search_et.getText().toString();
  14. if (!TextUtils.isEmpty(keyword)) {
  15. try {
  16. String result = NetworkUtil.doGet(BaseActivity.this, String.format(Global.API_SEARCH_SUGGESTIOIN_KEYWORDS, URLEncoder.encode(keyword, "utf-8")), false);
  17. Log.i("FetchSearchSuggestionKeywordsAsyncTask", result);
  18. if (!TextUtils.isEmpty(result)) {
  19. JSONArray array = new JSONArray(result);
  20. for (int i = 0; i < array.length(); i++) {
  21. JSONObject jsonObject = array.getJSONObject(i);
  22. rt.add(jsonObject.optString("keyword"));
  23. }
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. return rt;
  30. }
  31. @Override
  32. protected void onPostExecute(List<String> strings) {
  33. super.onPostExecute(strings);
  34. if (!strings.isEmpty()) {
  35. //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果
  36. searchSuggestionAdapter = new ArrayAdapter<String>(BaseActivity.this, android.R.layout.simple_dropdown_item_1line, strings);
  37. search_et.setAdapter(searchSuggestionAdapter);
  38. searchSuggestionAdapter.notifyDataSetChanged();
  39. }
  40. }
  41. }

ref:http://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered

扩展AutoCompleteTextView让其默认显示一组列表。setThreshold的更多相关文章

  1. DEDECMS点击主栏目默认显示第一个子栏目列表的方法

    本文实例讲述了DEDECMS点击主栏目默认显示第一个子栏目列表的方法.分享给大家供大家参考.具体分析如下: 今天公司有个需求是,点击导航上的父栏目进去默认显示第一个子栏目的列表,以下是具体实现方法,可 ...

  2. 请问:关于织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表的问题

    要设置织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表, 就按照如下图所示的方法进行操作,为什么 点击导航上的父栏目出现死循环呢, 根本浏览不了网页. 请各位大神指点指点,为什么点击 ...

  3. vue实现两重列表集合,点击显示,点击隐藏的折叠效果,(默认显示集合最新一条数据,点击展开,显示集合所有数据)

    效果图: 默认显示最新一条数据: 点击显示所有数据: 代码: 说明:这里主要是 这块用来控制显示或者隐藏 根据当前点击的  这个方法里传递的index 对应  isShow 数组里的index  ,对 ...

  4. Laravel大型项目系列教程(四)显示文章列表和用户修改文章

    小编心语:不知不觉已经第四部分了,非常感谢很多人给小编提的意见,改了很多bug,希望以后能继续帮小编找找茬~小编也不希望误导大家~这一节,主要讲的 是如何显示文章列表和让用户修改文章,小编预告一下(一 ...

  5. centos中设置apache显示目录列表

    apache中显示目录列表 在http.conf中加入如下代码(如有虚拟主机配置,加在虚拟主机配置段内),并把主目录内的index.pho,index.html,index.htm文件删除 复制代码  ...

  6. Android学习笔记:ListView简单应用--显示文字列表

    在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...

  7. ionic 下拉选择框中默认显示传入的参数

    开发过程当中遇到一个有趣的问题,如果我在第一个页面需要把 item { "ownerId" : 1 } 传递给第二个页面,并挂在$scope下 $scope.item = $sta ...

  8. /.nav-tabs :是普通标签页 .nav-pills:胶囊式标签页 action ;默认的激活项,给<li>加默认显示的是哪个标签页内容 .nav是标签页的一个基类,给ul加 .nav-stacked: 垂直排列BootStrap

    <meta name="viewport" content="with=device-width, initial-scale=1, user-scalabe=no ...

  9. Django - 权限(4)- queryset、二级菜单的默认显示、动态显示按钮权限

    一.queryset Queryset是django中构建的一种数据结构,ORM查询集往往是queryset数据类型,我们来进一步了解一下queryset的特点. 1.可切片 使用Python 的切片 ...

随机推荐

  1. sphinx配置

    配置文件 ## 数据源src1 source src1 { ## 说明数据源的类型.数据源的类型可以是:mysql,pgsql,mssql,xmlpipe,odbc,python ## 有人会奇怪,p ...

  2. 关于latch: cache buffers chains的sql优化

    前段时间,优化了一些耗buffer比较多的sql,但是CPU使用率还是没下来 . 查看操作系统CPU使用率 查看awr,发现又有一条超级耗性能的sql冒出来了. 该SQL每次执行耗费3e多个buffe ...

  3. buf.equals()

    buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...

  4. Python之条件判断

    Python之条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age ...

  5. Jmeter关联-获取token值

    1. token就是令牌,比如你授权(登录)一个程序时,他就是个依据,判断你是否已经授权该软件:也叫关联 2. cookie就是写在客户端的一个txt文件,里面包括你登录信息之类的,这样你下次在登录某 ...

  6. MVC系统学习5——验证

    其实关于Mvc的验证在上一篇已经有讲过一些了,可以通过在我们定义的Model上面添加相应的System.ComponentModel.DataAnnotations空间下的验证属性.在服务器端通过Mo ...

  7. SQL Server 2008如何创建定期自动备份任务

    我们知道,利用SQL Server 2008数据库可以实现数据库的定期自动备份.方法是用SQL SERVER 2008自带的维护计划创建一个计划对数据库进行备份,下面我们将SQL SERVER 200 ...

  8. UVALive 6430 (水dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. [NOIP2007] 提高组 洛谷P1099 树网的核

    题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并 ...

  10. RelativeLayout属性大全