?
分类:?Android?android实例2011-08-11 19:13?919人阅读?评论(1)?收藏?举报
androidlayoutdropdownstringencodinglistview
?
1、在布局文件当中声明一个AutoCompleteTextView
main.xml代码:
?
- <?xml?version="1.0"?encoding="utf-8"?>??
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"???
- ????android:orientation="horizontal"??
- ????android:layout_width="fill_parent"???
- ????android:layout_height="wrap_content"??
- ????android:padding="5dp">??
- ????<TextView??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="Country"?/>??
- ????<AutoCompleteTextView?android:id="@+id/autocomplete"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_marginLeft="5dp"/>??
- </LinearLayout>??
?
2、在res/layout下面创建一个新的布局文件:list_item.xml(定义下拉菜单中的条目的显示布局)
list_item.xml文件:
?
- <?xml?version="1.0"?encoding="utf-8"?>??
- <TextView?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:layout_width="fill_parent"??
- ????android:layout_height="fill_parent"??
- ????android:padding="10dp"??
- ????android:textSize="16sp"??
- ????android:textColor="#000">??
- </TextView>??
3、?AutoCompleteTextView需要使用ArrayAdapter来提供数据;
?
数据来源有两种方式:
1、在程序中动态获取:可以是数组;
?
- public?class?MainActivity?extends?Activity?{??
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????AutoCompleteTextView?autoCompleteTextView?=?null;??
- ????static?final?String[]?COUNTRIES?=?new?String[]?{??
- ??????????"Afghanistan",?"Albania",?"Algeria",?"American?Samoa",?"Andorra",??
- ??????????"Angola",?"Anguilla",?"Antarctica",?"Antigua?and?Barbuda",?"Argentina"??
- ????????};??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????//通过ID得到AutoCompleteTextView对象??
- ????????autoCompleteTextView?=?(AutoCompleteTextView)findViewById(R.id.autocomplete);??
- ????????//创建一个list,为ArrayAdapter提供数据??
- ????????List<String>?list?=?new?ArrayList<String>();??
- ????????list.add("测试测试");??
- ????????list.add("测试test");??
- ????????//创建一个ArrayAdapter对象??
- ????????ArrayAdapter<String>?arrayAdapter?=?new?ArrayAdapter<String>(this,R.layout.list_item,list);??
- ????????//将ArrayAdapter设置给AutoCompleteTextView对象??
- ????????autoCompleteTextView.setAdapter(arrayAdapter);??
- ??????????
- ????}??
- }??
?
2、在string.xml中定义;
string.xml文件:
?
- <?xml?version="1.0"?encoding="utf-8"?>??
- <resources>??
- ????<string-array?name="countries_array">??
- ????????<item>Bahrain</item>??
- ????????<item>Bangladesh</item>??
- ????????<item>Barbados</item>??
- ????????<item>Belarus</item>??
- ????????<item>Belgium</item>??
- ????????<item>Belize</item>??
- ????????<item>Benin</item>??
- ????</string-array>??
- </resources>??
?
?
- String[]?countries?=?getResources().getStringArray(R.array.countries_array);??
- ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,?R.layout.list_item,?countries);??
在许多 的控件中,都是用Adapter进行数据填充,如SimpleAdapter(ListView),ArrayAdapter等……
这样在输入框输入数据的时候自动填充;
?
如果我们要在输入提示的时候能进行多重提示,就需要用到它的子类MultiAutoCompleteTextView具体用法很简单只是添加了一个叫分离器的东西,代码如:
?
- public?class?AutocompleteActivity?extends?Activity?{??
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ??
- ????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,??
- ????????????????android.R.layout.simple_dropdown_item_1line,?COUNTRIES);??
- ????????MultiAutoCompleteTextView?textView?=?(MultiAutoCompleteTextView)?findViewById(R.id.edit);??
- ??????????
- ????????textView.setAdapter(adapter);??
- ????????textView.setThreshold(1);??
- ????????textView.setTokenizer(new?MultiAutoCompleteTextView.CommaTokenizer());??
- ????}??
- ??
- ????private?static?final?String[]?COUNTRIES?=?new?String[]?{??
- ????????"Belgium",?"France",?"Italy",?"Germany",?"Spain"??
- ????};??
- ?}??
?
?
这样写的话,系统默认分隔是逗号,并且在逗号后面还有一个空格,很难发现,如果不想用默认逗号和空格进行分割,这里需要重写MultiAutoCompleteTextView的子类CommaTokenizer,并实现接口Tokenizer(分离器),完整代码如下:?
- public?class?AutocompleteActivity?extends?Activity?{??
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,??
- ????????????????android.R.layout.simple_dropdown_item_1line,?COUNTRIES);??
- ????????MultiAutoCompleteTextView?textView?=?(MultiAutoCompleteTextView)?findViewById(R.id.edit);??
- ??????????
- ????????textView.setAdapter(adapter);??
- ????????textView.setThreshold(1);??
- ????????textView.setTokenizer(new?CommaTokenizer());??
- ????}??
- ??
- ????private?static?final?String[]?COUNTRIES?=?new?String[]?{??
- ????????"Belgium",?"France",?"Italy",?"Germany",?"Spain","Spain2"??
- ????};??
- ??????
- ??????
- ????public?class?CommaTokenizer?implements?Tokenizer{??
- ??
- ????????/**?
- ?????????*?在文本框中每输入任何一个字符都会调用这个方法?,返回的是每一个单词输入的开始位置(从0开始),cursor是最后面的之字符位置。?
- ?????????*/??
- ????????public?int?findTokenStart(CharSequence?text,?int?cursor)?{??
- ????????????int?i?=?cursor;??
- System.out.println("findTokenStart---"+text+"---cursor:"+cursor);??
- ????????????while?(i?>?0?&&?text.charAt(i?-?1)?!=?',')?{??
- ????????????????i--;??
- ????????????}??
- ????????????while?(i?<?cursor?&&?text.charAt(i)?==?'?')?{??
- ????????????????i++;??
- ????????????}??
- System.out.println(i);??
- ????????????return?i;??
- ????????}??
- ??????????
- ????????public?int?findTokenEnd(CharSequence?text,?int?cursor)?{??
- ????????????int?i?=?cursor;??
- ????????????int?len?=?text.length();??
- System.out.println("findTokenEnd---"+text+"---cursor:"+cursor);??
- ????????????while?(i?<?len)?{??
- ????????????????if?(text.charAt(i)?==?',')?{??
- ????????????????????return?i;??
- ????????????????}?else?{??
- ????????????????????i++;??
- ????????????????}??
- ????????????}??
- ??
- ????????????return?len;??
- ????????}??
- ????????/**?
- ?????????*?只有当回车结束输入的时候,才会调用些方法?。返回完整的字符串?
- ?????????*/??
- ????????public?CharSequence?terminateToken(CharSequence?text)?{??
- ????????????int?i?=?text.length();??
- System.out.println("terminateToken---"+text);??
- ????????????while?(i?>?0?&&?text.charAt(i?-?1)?==?'?')?{??
- ????????????????i--;??
- ????????????}??
- ??
- ????????????if?(i?>?0?&&?text.charAt(i?-?1)?==?',')?{??
- ????????????????return?text;??
- ????????????}?else?{??
- ????????????????if?(text?instanceof?Spanned)?{??
- ????????????????????SpannableString?sp?=?new?SpannableString(text?+?",?");??
- ????????????????????TextUtils.copySpansFrom((Spanned)?text,?0,?text.length(),??
- ????????????????????????????????????????????Object.class,?sp,?0);??
- ????????????????????return?sp;??
- ????????????????}?else?{??
- ????????????????????return?text?+?",?";??
- ????????????????}??
- ????????????}??
- ????????}??
- ????}??
- }??