扩展AutoCompleteTextView让其默认显示一组列表。

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

    /**
* <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();
}
}
}


ref:[url]http://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值