文字的自动提示功能

 AutoCompleteTextView控件   自动补全
            继承自EditText
   能够进行字符匹配
   其内部包含一个AdapterView对象,可以通过该控件的setAdapter方法,
       为AdapterView对象提供过滤数据
   setAdapter方法中接受的适配器必须实现Filterable接口
   主要方法:
         void setThreshold(int threshold)
 void setCompletionHint(CharSequence hint)
    MultiAutoComPleteTextView控件
              继承自 AutoCompleteTextView
       可以进行多次的提示
多次输入项使用指定的分词付间隔
主要方法:
  void setTokenizer(Tokenizer t)
  MultiAutoCompleteTextView类中包含一个Tokenizer

    接口的实现类CommaTokenizer

只能提示一次的小案例: 

      布局:

            

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <AutoCompleteTextView 
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

主Activity:

 

package com.example.auto;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

	private AutoCompleteTextView autv;
	private ArrayAdapter<String> adapter;
	private String[] words ={
			"good","very good","hellow","nihao",
			"how are you","what are you name"
	};
	
	private void setupView(){
		autv = (AutoCompleteTextView) findViewById(R.id.auto);
		autv.setThreshold(1);
		autv.setCompletionHint("历史信息...");
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,words);
		autv.setAdapter(adapter);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setupView();
	}
}

能够提示多个:

           布局:

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <MultiAutoCompleteTextView 
        android:id="@+id/mautv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
主Activity:

       

package com.example.auto;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends Activity {

	private String[] words ={
			"good","very good","hellow","nihao",
			"how are you","what are you name"
	};
	
	private MultiAutoCompleteTextView autv;
	private ArrayAdapter<String> adapter;
	private void setupView(){
		autv = (MultiAutoCompleteTextView)findViewById(R.id.mautv);
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,words);
		autv.setAdapter(adapter);
		autv.setThreshold(1);
		autv.setCompletionHint("搜索结果...");
		autv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setupView();
	}
}


你可能感兴趣的:(文字的自动提示功能)