android 中使用AutoCompleteTextView 可以实现自动提示功能

android 中使用AutoCompleteTextView 可以实现自动提示功能 

在java文档中预先设置好需要提示的字串,默认设置是输入两个字符的时候提醒,但是可以在java文件里面使用setThreshold() 方法实现更改提醒是输入的字符数,但是提醒的文字需要预先匹配
附上代码:
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools= "http://schemas.android.com/tools"   
   android:layout_width= "match_parent"  
   android:layout_height= "match_parent"  
   android:orientation= "vertical"
   tools:context= ".MainActivity" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />
 
    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </AutoCompleteTextView >
 
</LinearLayout>
 
java文件:
package com.just;
 
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
 
public class MainActivity extends Activity {
 
        private static final String[] autostr = new String[] { "a" , "abc" , "abcd" ,
                      "abcde", "abcdef" };
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout. activity_main);
              ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,
                           android.R.layout. simple_dropdown_item_1line, autostr );
              AutoCompleteTextView myautoview = (AutoCompleteTextView) findViewById(R.id.autocomplete );
              myautoview.setThreshold(3);
              myautoview.setAdapter(adapter);
 
       }
 
}
ps:在使用新窗口的同时一定要是用adapter来实现
原文地址:https://www.cnblogs.com/cwr941012/p/4909988.html