当前位置: 代码迷 >> Android >> Android:AutoCompleteTextView和MultiAutoCompleteTextView及逗号分隔有关问题
  详细解决方案

Android:AutoCompleteTextView和MultiAutoCompleteTextView及逗号分隔有关问题

热度:22   发布时间:2016-04-28 06:34:07.0
Android:AutoCompleteTextView和MultiAutoCompleteTextView及逗号分隔问题
?
分类:?Android?android实例?919人阅读?评论(1)?收藏?举报
androidlayoutdropdownstringencodinglistview

?

1、在布局文件当中声明一个AutoCompleteTextView

main.xml代码:

?

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"???
  3. ????android:orientation="horizontal"??
  4. ????android:layout_width="fill_parent"???
  5. ????android:layout_height="wrap_content"??
  6. ????android:padding="5dp">??
  7. ????<TextView??
  8. ????????android:layout_width="wrap_content"??
  9. ????????android:layout_height="wrap_content"??
  10. ????????android:text="Country"?/>??
  11. ????<AutoCompleteTextView?android:id="@+id/autocomplete"??
  12. ????????android:layout_width="fill_parent"??
  13. ????????android:layout_height="wrap_content"??
  14. ????????android:layout_marginLeft="5dp"/>??
  15. </LinearLayout>??

?

2、在res/layout下面创建一个新的布局文件:list_item.xml(定义下拉菜单中的条目的显示布局)

list_item.xml文件:

?

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <TextView?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:layout_width="fill_parent"??
  4. ????android:layout_height="fill_parent"??
  5. ????android:padding="10dp"??
  6. ????android:textSize="16sp"??
  7. ????android:textColor="#000">??
  8. </TextView>??

3、?AutoCompleteTextView需要使用ArrayAdapter来提供数据;

?

数据来源有两种方式:

1、在程序中动态获取:可以是数组;

?

[java]?view plaincopy
?
  1. public?class?MainActivity?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????AutoCompleteTextView?autoCompleteTextView?=?null;??
  4. ????static?final?String[]?COUNTRIES?=?new?String[]?{??
  5. ??????????"Afghanistan",?"Albania",?"Algeria",?"American?Samoa",?"Andorra",??
  6. ??????????"Angola",?"Anguilla",?"Antarctica",?"Antigua?and?Barbuda",?"Argentina"??
  7. ????????};??
  8. ????@Override??
  9. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  10. ????????super.onCreate(savedInstanceState);??
  11. ????????setContentView(R.layout.main);??
  12. ????????//通过ID得到AutoCompleteTextView对象??
  13. ????????autoCompleteTextView?=?(AutoCompleteTextView)findViewById(R.id.autocomplete);??
  14. ????????//创建一个list,为ArrayAdapter提供数据??
  15. ????????List<String>?list?=?new?ArrayList<String>();??
  16. ????????list.add("测试测试");??
  17. ????????list.add("测试test");??
  18. ????????//创建一个ArrayAdapter对象??
  19. ????????ArrayAdapter<String>?arrayAdapter?=?new?ArrayAdapter<String>(this,R.layout.list_item,list);??
  20. ????????//将ArrayAdapter设置给AutoCompleteTextView对象??
  21. ????????autoCompleteTextView.setAdapter(arrayAdapter);??
  22. ??????????
  23. ????}??
  24. }??

?

2、在string.xml中定义;

string.xml文件:

?

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <resources>??
  3. ????<string-array?name="countries_array">??
  4. ????????<item>Bahrain</item>??
  5. ????????<item>Bangladesh</item>??
  6. ????????<item>Barbados</item>??
  7. ????????<item>Belarus</item>??
  8. ????????<item>Belgium</item>??
  9. ????????<item>Belize</item>??
  10. ????????<item>Benin</item>??
  11. ????</string-array>??
  12. </resources>??
在程序中获取数据资源:

?

?

[java]?view plaincopy
?
  1. String[]?countries?=?getResources().getStringArray(R.array.countries_array);??
  2. ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,?R.layout.list_item,?countries);??

在许多 的控件中,都是用Adapter进行数据填充,如SimpleAdapter(ListView),ArrayAdapter等……

这样在输入框输入数据的时候自动填充;

?

如果我们要在输入提示的时候能进行多重提示,就需要用到它的子类MultiAutoCompleteTextView具体用法很简单只是添加了一个叫分离器的东西,代码如:

?

[java]?view plaincopy
?
  1. public?class?AutocompleteActivity?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????@Override??
  4. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  5. ????????super.onCreate(savedInstanceState);??
  6. ????????setContentView(R.layout.main);??
  7. ??
  8. ????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,??
  9. ????????????????android.R.layout.simple_dropdown_item_1line,?COUNTRIES);??
  10. ????????MultiAutoCompleteTextView?textView?=?(MultiAutoCompleteTextView)?findViewById(R.id.edit);??
  11. ??????????
  12. ????????textView.setAdapter(adapter);??
  13. ????????textView.setThreshold(1);??
  14. ????????textView.setTokenizer(new?MultiAutoCompleteTextView.CommaTokenizer());??
  15. ????}??
  16. ??
  17. ????private?static?final?String[]?COUNTRIES?=?new?String[]?{??
  18. ????????"Belgium",?"France",?"Italy",?"Germany",?"Spain"??
  19. ????};??
  20. ?}??

?

?

这样写的话,系统默认分隔是逗号,并且在逗号后面还有一个空格,很难发现,如果不想用默认逗号和空格进行分割,这里需要重写MultiAutoCompleteTextView的子类CommaTokenizer,并实现接口Tokenizer(分离器),完整代码如下:

?

[java]?view plaincopy
?
  1. public?class?AutocompleteActivity?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????@Override??
  4. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  5. ????????super.onCreate(savedInstanceState);??
  6. ????????setContentView(R.layout.main);??
  7. ????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,??
  8. ????????????????android.R.layout.simple_dropdown_item_1line,?COUNTRIES);??
  9. ????????MultiAutoCompleteTextView?textView?=?(MultiAutoCompleteTextView)?findViewById(R.id.edit);??
  10. ??????????
  11. ????????textView.setAdapter(adapter);??
  12. ????????textView.setThreshold(1);??
  13. ????????textView.setTokenizer(new?CommaTokenizer());??
  14. ????}??
  15. ??
  16. ????private?static?final?String[]?COUNTRIES?=?new?String[]?{??
  17. ????????"Belgium",?"France",?"Italy",?"Germany",?"Spain","Spain2"??
  18. ????};??
  19. ??????
  20. ??????
  21. ????public?class?CommaTokenizer?implements?Tokenizer{??
  22. ??
  23. ????????/**?
  24. ?????????*?在文本框中每输入任何一个字符都会调用这个方法?,返回的是每一个单词输入的开始位置(从0开始),cursor是最后面的之字符位置。?
  25. ?????????*/??
  26. ????????public?int?findTokenStart(CharSequence?text,?int?cursor)?{??
  27. ????????????int?i?=?cursor;??
  28. System.out.println("findTokenStart---"+text+"---cursor:"+cursor);??
  29. ????????????while?(i?>?0?&&?text.charAt(i?-?1)?!=?',')?{??
  30. ????????????????i--;??
  31. ????????????}??
  32. ????????????while?(i?<?cursor?&&?text.charAt(i)?==?'?')?{??
  33. ????????????????i++;??
  34. ????????????}??
  35. System.out.println(i);??
  36. ????????????return?i;??
  37. ????????}??
  38. ??????????
  39. ????????public?int?findTokenEnd(CharSequence?text,?int?cursor)?{??
  40. ????????????int?i?=?cursor;??
  41. ????????????int?len?=?text.length();??
  42. System.out.println("findTokenEnd---"+text+"---cursor:"+cursor);??
  43. ????????????while?(i?<?len)?{??
  44. ????????????????if?(text.charAt(i)?==?',')?{??
  45. ????????????????????return?i;??
  46. ????????????????}?else?{??
  47. ????????????????????i++;??
  48. ????????????????}??
  49. ????????????}??
  50. ??
  51. ????????????return?len;??
  52. ????????}??
  53. ????????/**?
  54. ?????????*?只有当回车结束输入的时候,才会调用些方法?。返回完整的字符串?
  55. ?????????*/??
  56. ????????public?CharSequence?terminateToken(CharSequence?text)?{??
  57. ????????????int?i?=?text.length();??
  58. System.out.println("terminateToken---"+text);??
  59. ????????????while?(i?>?0?&&?text.charAt(i?-?1)?==?'?')?{??
  60. ????????????????i--;??
  61. ????????????}??
  62. ??
  63. ????????????if?(i?>?0?&&?text.charAt(i?-?1)?==?',')?{??
  64. ????????????????return?text;??
  65. ????????????}?else?{??
  66. ????????????????if?(text?instanceof?Spanned)?{??
  67. ????????????????????SpannableString?sp?=?new?SpannableString(text?+?",?");??
  68. ????????????????????TextUtils.copySpansFrom((Spanned)?text,?0,?text.length(),??
  69. ????????????????????????????????????????????Object.class,?sp,?0);??
  70. ????????????????????return?sp;??
  71. ????????????????}?else?{??
  72. ????????????????????return?text?+?",?";??
  73. ????????????????}??
  74. ????????????}??
  75. ????????}??
  76. ????}??
  77. }??
  相关解决方案