Android控件复习之实现动态自动匹配的内容
实现动态自动匹配的内容
AutoCompleteTextView 动态匹配输入的内容
AutoCompleteTextView 的特殊属性:
android:completionThreshold=“3”
这是设置输入多少字符时自动匹配
Demo
public class MainActivity extends Activity {
private AutoCompleteTextView autoCompleteTextView;
private String[] ar;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.completeTextView);
ar = new String[]{"jiayuan", "jiazheng", "chenshi", "chenzheng"};
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar);
autoCompleteTextView.setAdapter(arrayAdapter);
}
}
xml:
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/completeTextView"/>
MultiAutoCompleteTextView 可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配,可以用在发短信、发邮件时选择联系人这种类型中。
除了跟AutoCompleteTextView的特殊属性一样之外,MultiAutoCompleteTextView还有设置分隔符这个属性:
private MultiAutoCompleteTextView mt;
mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Demo
public class MainActivity extends Activity {
private MultiAutoCompleteTextView multiAutoCompleteTextView;
private String[] ar;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.completeTextView);
ar = new String[]{"jiayuan", "jiazheng", "chenshi", "chenzheng"};
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar);
multiAutoCompleteTextView.setAdapter(arrayAdapter);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
xml:
<MultiAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/completeTextView"
android:hint="请输入"/>