zoukankan      html  css  js  c++  java
  • 扩展AutoCompleteTextView让其默认显示一组列表。setThreshold

     

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

    Java代码  收藏代码
    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方法, 就能在不输入任何字符的情况下显示下拉框。 

    Java代码  收藏代码
    1. package com.wole.android.pad.view;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Rect;  
    5. import android.util.AttributeSet;  
    6. import android.widget.AutoCompleteTextView;  
    7.   
    8. /** 
    9.  * Created with IntelliJ IDEA. 
    10.  * User: denny 
    11.  * Date: 12-12-4 
    12.  * Time: 下午2:16 
    13.  * To change this template use File | Settings | File Templates. 
    14.  */  
    15. public class InstantAutoComplete extends AutoCompleteTextView {  
    16.     private int myThreshold;  
    17.   
    18.     public InstantAutoComplete(Context context) {  
    19.         super(context);  
    20.     }  
    21.   
    22.     public InstantAutoComplete(Context context, AttributeSet attrs) {  
    23.         super(context, attrs);  
    24.     }  
    25.   
    26.     public InstantAutoComplete(Context context, AttributeSet attrs, int defStyle) {  
    27.         super(context, attrs, defStyle);  
    28.     }  
    29.   
    30.     @Override  
    31.     public boolean enoughToFilter() {  
    32.         return true;  
    33.     }  
    34.   
    35.     @Override  
    36.     protected void onFocusChanged(boolean focused, int direction,  
    37.                                   Rect previouslyFocusedRect) {  
    38.         super.onFocusChanged(focused, direction, previouslyFocusedRect);  
    39.         if (focused) {  
    40.             performFiltering(getText(), 0);  
    41.             showDropDown();  
    42.         }  
    43.     }  
    44.   
    45.     public void setThreshold(int threshold) {  
    46.         if (threshold < 0) {  
    47.             threshold = 0;  
    48.         }  
    49.         myThreshold = threshold;  
    50.     }  
    51.   
    52.     public int getThreshold() {  
    53.         return myThreshold;  
    54.     }  
    55. }  


    Java代码  收藏代码
    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.   
    8.        @Override  
    9.        public void onTextChanged(CharSequence s, int start, int before, int count) {  
    10.        }  
    11.   
    12. 没有输入任何东西 则显示默认列表,否则调用接口,展示下拉列表  
    13.        @Override  
    14.        public void afterTextChanged(Editable s) {  
    15.            if (s.length() >= 1) {  
    16.                if (fetchSearchSuggestionKeywordsAsyncTask != null) {  
    17.                    fetchSearchSuggestionKeywordsAsyncTask.cancel(true);  
    18.                }  
    19.                fetchSearchSuggestionKeywordsAsyncTask =new FetchSearchSuggestionKeywordsAsyncTask();  
    20.                fetchSearchSuggestionKeywordsAsyncTask.execute();  
    21.            }else{  
    22.                showHotSearchKeywords();  
    23.            }  
    24.   
    25.        }  
    26.    });  
    27.   
    28.    search_et.setOnItemClickListener(new OnItemClickListener() {  
    29.        @Override  
    30.        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
    31.            String item = searchSuggestionAdapter.getItem(position);  
    32.            search_et.setText(item);  
    33.            search_btn.performClick();  
    34.        }  
    35.    });  
    36.   
    37.     //点击autocompletetextview时,如果没有输入任何东西 则显示默认列表  
    38.    search_et.setOnTouchListener(new View.OnTouchListener() {  
    39.        @Override  
    40.        public boolean onTouch(View v, MotionEvent event) {  
    41.            if (TextUtils.isEmpty(search_et.getText().toString())) {  
    42.                showHotSearchKeywords();  
    43.            }  
    44.            return false;  
    45.        }  
    46.   
    47.    });  


    Java代码  收藏代码
    1.    
    2. //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果  
    3. private void showHotSearchKeywords() {  
    4.         MiscUtil.prepareHotSearchKeywords(getWoleApplication());  
    5.         searchSuggestionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getWoleApplication().hotSearchHistoryKeywords);  
    6.         search_et.setAdapter(searchSuggestionAdapter);  
    7.         searchSuggestionAdapter.notifyDataSetChanged();  
    8.         search_et.showDropDown();  
    9.     }  
    10.   
    11.     private class FetchSearchSuggestionKeywordsAsyncTask extends AsyncTask<Void, Void, List<String>> {  
    12.   
    13.         @Override  
    14.         protected List<String> doInBackground(Void... params) {  
    15.             List<String> rt = new ArrayList<String>(5);  
    16.             String keyword = search_et.getText().toString();  
    17.             if (!TextUtils.isEmpty(keyword)) {  
    18.                 try {  
    19.                     String result = NetworkUtil.doGet(BaseActivity.this, String.format(Global.API_SEARCH_SUGGESTIOIN_KEYWORDS, URLEncoder.encode(keyword, "utf-8")), false);  
    20.                     Log.i("FetchSearchSuggestionKeywordsAsyncTask", result);  
    21.                     if (!TextUtils.isEmpty(result)) {  
    22.                         JSONArray array = new JSONArray(result);  
    23.                         for (int i = 0; i < array.length(); i++) {  
    24.                             JSONObject jsonObject = array.getJSONObject(i);  
    25.                             rt.add(jsonObject.optString("keyword"));  
    26.                         }  
    27.                     }  
    28.                 } catch (Exception e) {  
    29.                     e.printStackTrace();  
    30.                 }  
    31.             }  
    32.             return rt;  
    33.         }  
    34.   
    35.         @Override  
    36.         protected void onPostExecute(List<String> strings) {  
    37.             super.onPostExecute(strings);  
    38.             if (!strings.isEmpty()) {  
    39. //这里发现很奇怪的事情, 需要每次new一个ArrayAdapter,要不然有时调用showDropDown不会有效果  
    40.                 searchSuggestionAdapter = new ArrayAdapter<String>(BaseActivity.this, android.R.layout.simple_dropdown_item_1line, strings);  
    41.                 search_et.setAdapter(searchSuggestionAdapter);  
    42.                 searchSuggestionAdapter.notifyDataSetChanged();  
    43.             }  
    44.         }  
    45.     }  


    ref:http://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered
  • 相关阅读:
    2.替换空格
    1.二维数组的查找
    poj 2431 expedition
    python入门第三天
    python入门第二天__练习题
    [Python3.6] print vs sys.stdout.write
    python入门第二天
    使用Flask-mail发送邮件无法连接主机
    KMP
    逆序对 线段树&树状数组 (重制版)
  • 原文地址:https://www.cnblogs.com/qianyukun/p/7454095.html
Copyright © 2011-2022 走看看