AutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息。

当然这里要要用到一些适配器


Android中 提供了两智能输入框,它们是MultiAutoCompleteTextView、AutoCompleteTextView。它们的功能大致一样,它和 AutoCompleteTextView的区别就是MultiAutoCompleteTextView可以在输入框中一直增加新的选取值。编写方式也 有所不同,在进行setAdapter之后还需要调用setTokenizer() 。下面详细介绍一下。
一、AutoCompleteTextView
1.简介
一个可编辑的文本视图,当用户输入信息后弹出提示。提示列表显示在一个下拉菜单中,用户可以从中选择一项,以完成输入。提示列表是从一个数据适配器获取的数据。
2.重要方法
      clearListSelection():清除选中的列表项
      dismissDropDown():如果存在关闭下拉菜单
      getAdapter():获取适配器
3.创建须知
 (1)布局文件
<AutoCompleteTextView
        android: />   
  (2)程序
    实例化适配器
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);
    设置适配器
      edit.setAdapter(adapter);
    确定范围
    edit1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer())

下面是个例子

AutoCommitTest.java

Java代码

  1. package org.hualang.auto;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.AutoCompleteTextView;
  6. import android.widget.MultiAutoCompleteTextView;
  7. public class AutoCommitTest extends Activity {
  8.     /** Called when the activity is first created. */
  9.         private static final String[] autoString=new String[]{"welcome","well",
  10.                 "weatch","weexeview","werap"};                                    
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.         //关联关键字
  16.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
  17.                         android.R.layout.simple_dropdown_item_1line,
  18.                         autoString);
  19.         AutoCompleteTextView autocomplete=(AutoCompleteTextView)findViewById(R.id.auto);
  20.         autocomplete.setAdapter(adapter);
  21.         MultiAutoCompleteTextView multi=(MultiAutoCompleteTextView) findViewById(R.id.multi);
  22.         //将adapter添加到AutoCompleteTextView中
  23.         multi.setAdapter(adapter);
  24.         multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
  25.     }
  26. }
复制代码



main.xml

Java代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView  
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="自动提示功能演示"
  11.     />
  12. <AutoCompleteTextView
  13.         android:
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="wrap_content"
  16. />
  17. <MultiAutoCompleteTextView
  18.         android:
  19.         android:layout_width="fill_parent"
  20.         android:layout_height="wrap_content"
  21. />
  22. </LinearLayout>
复制代码



运行结果如下:

<ignore_js_op>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2021-10-02
  • 2021-08-11
相关资源
相似解决方案