EditText 自动完成文本填充

//安卓的编辑框输入2个字母实现自动补充文本的效果
public class MainActivity extends Activity {

private static final String[] cities=new String[]
       {"FuZhou", "XiaMen", "NiDe", "PuTian","QuanZhou", "ZhangZhou", "LongYan", "SanMing","NanPing"};
   private AutoCompleteTextView autoCompleteTextView=null;
   private MultiAutoCompleteTextView multiAutoCompleteTextView=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
    multiAutoCompleteTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
       ArrayAdapter adapter=new ArrayAdapter(
               this, 
               android.R.layout.simple_dropdown_item_1line, 
               cities);
       autoCompleteTextView.setAdapter(adapter);
       //设置输入多少字符后提示,默认值为2
       autoCompleteTextView.setThreshold(2);
       
       multiAutoCompleteTextView.setAdapter(adapter);
       multiAutoCompleteTextView.setThreshold(2);
       //用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。
       multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
   }

}

//布局layout的代码如下:


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
    android:orientation="vertical" >


            android:id="@+id/autoCompleteTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


            android:id="@+id/multiAutoCompleteTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


EditText 自动完成文本填充_第1张图片

你可能感兴趣的:(EditText)