这篇文章给大家介绍Android开发中怎么实现一个输入框提示功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
可以使用cursor
来动态加载AutoCompleteTextView的数据,从而 实现时时搜索提示,要实现动态加载,只用重写一个类继承于CursorAdapter
,然后设定在AutoCompleteTextView上就行了。
AutoCompleteTextView editNumber = (AutoCompleteTextView)findViewById(R.id.edit_number);
Cursor cursor = getContentResolver()(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
ContactListAdapter listAdapter = new ContactListAdapter(this, cursor);
editNumber.setAdapter(listAdapter);
ContactListAdapter.java中的核心代码如下:
重写newView方法
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = (View)inflater.inflate( R.layout.auto_complete, parent, false);
TextView txtName = (TextView)view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(0));
TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
txtNumber.setText(cursor.getString(1));
TextView txtType = (TextView)view.findViewById(R.id.txt_type);
String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
if(cursor.getint(2) > 3)
{
txtType.setText(arrType[0]);
} else
{
txtType.setText(arrType[cursor.getint(2)]);
}
return view;
}
重写bindView方法,
public void bindView(View view, Context context, Cursor cursor) {
TextView txtName = (TextView)view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(0));
TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
txtNumber.setText(cursor.getString(1));
TextView txtType = (TextView)view.findViewById(R.id.txt_type);
String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
if(cursor.getint(2) > 3)
{
txtType.setText(arrType[0]);
} else {
txtType.setText(arrType[cursor.getint(2)]);
}
}
点击弹出的Listview列表后的返回值:
public String convertToString(Cursor cursor) {}
执行搜索的sql语句,返回一个Cursor加载到弹出的Listview上
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {}
在此所返回的Cursor结果,会全部显示在弹出提示上,无需再次过虑。
关于Android开发中怎么实现一个输入框提示功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。