在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的。
AutoCompleteTextView常用属性 | |
---|---|
android:completionHint | 设置出现在下拉菜单中的提示标题 |
android:completionThreshold | 设置用户至少输入多少个字符才会显示提示 |
android:dropDownHorizontalOffset | 下拉菜单于文本框之间的水平偏移。默认与文本框左对齐 |
android:dropDownHeight | 下拉菜单的高度 |
android:dropDownWidth | 下拉菜单的宽度 |
android:singleLine | 单行显示 |
android:dropDownVerticalOffset | 垂直偏移量 |
<AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/autotext" />2,在代码中要设置adpater适配器,我这里用CursorAdapter的子类
class MyContactsAdapter extends CursorAdapter {
private static final int NUM_INDEX = 0;
public MyContactsAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
<span style="white-space:pre"> </span>//显示的条目布局
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return View.inflate(getApplicationContext(), R.layout.item_auto_contact, null);
}
<span style="white-space:pre"> </span>//绑定数据
@Override
public void bindView(View view, Context context, Cursor cursor) {
String name = cursor.getString(NAME_INDEX);
String num = cursor.getString(DATA1_INDEX);
TextView name_txt = (TextView) view.findViewById(R.id.txt_name);
TextView num_txt = (TextView) view.findViewById(R.id.txt_num);
name_txt.setText(name);
num_txt.setText(num);
}
/*
* 当自动提示文本框开始过滤查询时回调
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
new String[] { "_id", "data1", "display_name" }, "data1 like ?",
new String[] { (String) constraint + "%" }, null);
Utils.print(cursor);
return cursor;
}
/*
* 把点击的游标换成字符串
*/
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(DATA1_INDEX);
}
}