AutoCompleteTextView自动提示文本的使用

在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的。

AutoCompleteTextView常用属性
android:completionHint 设置出现在下拉菜单中的提示标题
android:completionThreshold 设置用户至少输入多少个字符才会显示提示
android:dropDownHorizontalOffset 下拉菜单于文本框之间的水平偏移。默认与文本框左对齐
android:dropDownHeight 下拉菜单的高度
android:dropDownWidth 下拉菜单的宽度
android:singleLine 单行显示
android:dropDownVerticalOffset 垂直偏移量
1,在xml中布局

<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);
		}

	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值