您的位置:首页 > 其它

AutoCompleteTextView输入两个字符后才给提示列表

2015-04-16 11:03 323 查看
在Android中使用自动提示输入,控件为AutoCompleteTextView类型对象autoText,当给autoText设置适配器时,继承自ListAdapter或者BaseAdapter,需要实现接口Filter,并且自定义实现Filter子类。

public class AutoCompleteAdapter extends BaseAdapter implements Filterable{

private Context context;
private ArrayFilter mFilter;
private List<String> mObjects;//过滤后的item
private final Object mLock = new Object();
private int maxMatch = 100;//最多显示多少个选项,负数表示全部

public AutoCompleteAdapter(Context context) {
this.context = context;
}

@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}

private class ArrayFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence prefix) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults();
if (prefix == null || prefix.length() == 0) {
synchronized (mLock) {
ArrayList<String> list = new ArrayList<String>();
results.values = list;
results.count = list.size();
return results;
}
} else {
String prefixString = prefix.toString();

final ArrayList<String> newValues = new ArrayList<String>();
...
results.values = newValues;
results.count = newValues.size();
}
return results;
}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
mObjects = (List<String>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mObjects.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
//此方法有误,尽量不要使用
return mObjects.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_login_users, null);
holder.tv = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.tv.setText(mObjects.get(position));
return convertView;
}

class ViewHolder {
TextView tv;
}
给autoText设置适配器

autoText.setAdapter(new AutoCompleteAdapter(getApplicationContext()));

这时,发现只有在输入字符超过两个字符时才会给出下拉提示列表。在AutoCompleteTextView源码中找到处理文字变更的方法:

void doAfterTextChanged() {
if (mBlockCompletion) return;

// if the list was open before the keystroke, but closed afterwards,
// then something in the keystroke processing (an input filter perhaps)
// called performCompletion() and we shouldn't do any more processing.
if (DEBUG) Log.v(TAG, "after text changed: openBefore=" + mOpenBefore
+ " open=" + isPopupShowing());
if (mOpenBefore && !isPopupShowing()) {
return;
}

// the drop down is shown only when a minimum number of characters
// was typed in the text view
if (enoughToFilter()) {
if (mFilter != null) {
mPopupCanBeUpdated = true;
performFiltering(getText(), mLastKeyCode);
}
} else {
// drop down is automatically dismissed when enough characters
// are deleted from the text view
if (!mPopup.isDropDownAlwaysVisible()) {
dismissDropDown();
}
if (mFilter != null) {
mFilter.filter(null);
}
}
}
这个方法里有一个if(enoughFilter())的判断,为true时才对字符进行判断,为false直接传null给filter()方法;那个这个enoughFilter()是什么呢?看下面代码:

/**
* Returns <code>true</code> if the amount of text in the field meets
* or exceeds the {@link #getThreshold} requirement.  You can override
* this to impose a different standard for when filtering will be
* triggered.
*/
public boolean enoughToFilter() {
if (DEBUG) Log.v(TAG, "Enough to filter: len=" + getText().length()
+ " threshold=" + mThreshold);
return getText().length() >= mThreshold;
}
这个方法,判断的是变更后文字的长度跟mThreshold这个值的大小比较,mThreshold是一个private的整型变量;这个变量有个默认的初始值为2:

public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

...

TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AutoCompleteTextView, defStyle, 0);

mThreshold = a.getInt(
R.styleable.AutoCompleteTextView_completionThreshold, 2);

...

a.recycle();

...
}
看到这里,就明白了,需要设置Threshold的初始值,这个初始可以在XML中设置,也可以在Java代码中设置。

android:completionThreshold="1"
autoText.setThreshold(1);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐