分类: Android android实例   919人阅读  评论(1)  收藏  举报 android layout dropdown string encoding listview

 

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

main.xml代码:

 

[html]  view plain copy  
  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 plain copy  
  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 plain copy  
  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 list = new ArrayList();  
  16.         list.add("测试测试");  
  17.         list.add("测试test");  
  18.         //创建一个ArrayAdapter对象  
  19.         ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_item,list);  
  20.         //将ArrayAdapter设置给AutoCompleteTextView对象  
  21.         autoCompleteTextView.setAdapter(arrayAdapter);  
  22.           
  23.     }  
  24. }  

 

2、在string.xml中定义;

string.xml文件:

 

[html]  view plain copy  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="countries_array">  
  4.         <item>Bahrainitem>  
  5.         <item>Bangladeshitem>  
  6.         <item>Barbadositem>  
  7.         <item>Belarusitem>  
  8.         <item>Belgiumitem>  
  9.         <item>Belizeitem>  
  10.         <item>Beninitem>  
  11.     string-array>  
  12. resources>  
在程序中获取数据资源:

 

 

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

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

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

 

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

 

[java]  view plain copy  
  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 adapter = new ArrayAdapter(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 plain copy  
  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 adapter = new ArrayAdapter(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. }  

更多相关文章

  1. Android(安卓)Bundle类
  2. 6.1、Android中从Internet获取数据
  3. Android(安卓)Jetpack-ViewModel
  4. 使用saripaar对android输入控件进行快速验证
  5. android中的sqlit3数据库进行手机应用软件开发(自写的一个财务管
  6. Android
  7. Android(安卓)Bundle类
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. python起点网月票榜字体反爬案例

随机推荐

  1. Android(安卓)SDK 在线更新镜像服务器资
  2. android 自定义数字软键盘
  3. android aapt使用小结
  4. android 音频framework的分析
  5. Android(安卓)动画之ScaleAnimation应用
  6. Android(安卓)Studio 编译release的aar、
  7. android ListView
  8. android自带图片资源图标一览,android.R.d
  9. 仿微信摇一摇功能,android 重力感应开发
  10. android studio 简单二维码扫描实现