输入提示——AutoCompleteTextView与MultiAutoCompleteTextView-自动完成文本与多自动完成文本

AutoCompleteTextView:能实现文本输入时给出提示

AutoCompleteTextView auto=(AutoCompleteTextView) findViewById(id.auto);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this,   R.array.auto,   android.R.layout.simple_list_item_1);//资源与显示样式
auto.setAdapter(adapter);//设置适配器

资源文件如下:写在values下的string.xml中:

<array name="auto">
  <item >shenzheng</item>
  <item >shooo</item>
  <item >cheng</item>
  <item >abbbb</item>
  <item >abbcc</item>
</array>

布局xml文件如下:

<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="@drawable/ic_launcher"
android:completionThreshold="1"     //代表至少输入1个字符后给出提示
android:hint="请输入地名" />

 

MultiAutoCompleteTextView 能实现同时输入多个时给出提示 比如: 文本1,文本2      “,”代表分隔符  输入文本1和文本2时都能给出提示

设置分离器 setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer() );//将分离器设置为逗号

 

popupBackground 下拉菜单的背景

dropDownWidth 下拉菜单的宽度

completionThreshold 指定用户至少输入几个字符才会显示提示

textCursorDrawable="@null" 设置光标颜色与文本颜色一致

ems 指定补全框的宽度

监听事件:

 

//自动补全编辑框的使用
        AutoCompleteTextView auto=(AutoCompleteTextView) findViewById(R.id.autoComplete);
        String[] arr=getResources().getStringArray(R.array.arr);
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, arr);
        auto.setAdapter(adapter2);
        auto.addTextChangedListener(new TextWatcher() {
            
            @Override//count:1代表当前操作为增加数据     0代表当前操作为删除数据
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("改变时:"+s+"--"+start+"--"+before+"--"+count);
            }
            
            @Override//当前状态的前一个状态的值
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                System.out.println("改变前:"+s+"--"+start+"--"+count);
            }
            
            @Override//输入框输入后的值
            public void afterTextChanged(Editable s) {
                System.out.println("改变后:"+s);
            }
        });

 

posted @ 2016-04-24 16:40  ts-android  阅读(219)  评论(0编辑  收藏  举报