android 自动填充文本框,android – 自动填充文本框,突出显示建议列表中的键入字符...

我已经实现了功能.解决方案如下:

AutoCompleteAdapter.java

public class AutoCompleteAdapter extends ArrayAdapter implements

Filterable {

private ArrayList fullList;

private ArrayList mOriginalValues;

private ArrayFilter mFilter;

LayoutInflater inflater;

String text = "";

public AutoCompleteAdapter(Context context, int resource,

int textViewResourceId, List objects) {

super(context, resource, textViewResourceId, objects);

fullList = (ArrayList) objects;

mOriginalValues = new ArrayList(fullList);

inflater = LayoutInflater.from(context);

}

@Override

public int getCount() {

return fullList.size();

}

@Override

public String getItem(int position) {

return fullList.get(position);

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;

// tvViewResourceId = (TextView) view.findViewById(android.R.id.text1);

String item = getItem(position);

Log.d("item", "" + item);

if (convertView == null) {

convertView = view = inflater.inflate(

android.R.layout.simple_dropdown_item_1line, null);

}

// Lookup view for data population

TextView myTv = (TextView) convertView.findViewById(android.R.id.text1);

myTv.setText(highlight(text, item));

return view;

}

@Override

public Filter getFilter() {

if (mFilter == null) {

mFilter = new ArrayFilter();

}

return mFilter;

}

private class ArrayFilter extends Filter {

private Object lock;

@Override

protected FilterResults performFiltering(CharSequence prefix) {

FilterResults results = new FilterResults();

if (prefix != null) {

text = prefix.toString();

}

if (mOriginalValues == null) {

synchronized (lock) {

mOriginalValues = new ArrayList(fullList);

}

}

if (prefix == null || prefix.length() == 0) {

synchronized (lock) {

ArrayList list = new ArrayList(

mOriginalValues);

results.values = list;

results.count = list.size();

}

} else {

final String prefixString = prefix.toString().toLowerCase();

ArrayList values = mOriginalValues;

int count = values.size();

ArrayList newValues = new ArrayList(count);

for (int i = 0; i < count; i++) {

String item = values.get(i);

if (item.toLowerCase().contains(prefixString)) {

newValues.add(item);

}

}

results.values = newValues;

results.count = newValues.size();

}

return results;

}

@SuppressWarnings("unchecked")

@Override

protected void publishResults(CharSequence constraint,

FilterResults results) {

if (results.values != null) {

fullList = (ArrayList) results.values;

} else {

fullList = new ArrayList();

}

if (results.count > 0) {

notifyDataSetChanged();

} else {

notifyDataSetInvalidated();

}

}

}

public static CharSequence highlight(String search, String originalText) {

// ignore case and accents

// the same thing should have been done for the search text

String normalizedText = Normalizer

.normalize(originalText, Normalizer.Form.NFD)

.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")

.toLowerCase(Locale.ENGLISH);

int start = normalizedText.indexOf(search.toLowerCase(Locale.ENGLISH));

if (start < 0) {

// not found, nothing to to

return originalText;

} else {

// highlight each appearance in the original text

// while searching in normalized text

Spannable highlighted = new SpannableString(originalText);

while (start >= 0) {

int spanStart = Math.min(start, originalText.length());

int spanEnd = Math.min(start + search.length(),

originalText.length());

highlighted.setSpan(new ForegroundColorSpan(Color.BLUE),

spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

start = normalizedText.indexOf(search, spanEnd);

}

return highlighted;

}

}

}

MainActivity.java

public class MainActivity extends Activity {

String[] languages = { "C", "C++", "Java", "C#", "PHP", "JavaScript",

"jQuery", "AJAX", "JSON" };

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

List wordList = new ArrayList();

Collections.addAll(wordList, languages);

AutoCompleteAdapter adapter = new AutoCompleteAdapter(this,

android.R.layout.simple_dropdown_item_1line,

android.R.id.text1,wordList);

AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);

acTextView.setThreshold(1);

acTextView.setAdapter(adapter);

}

}

像魅力一样工作!

请享用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值