RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Android开发中怎么实现一个输入框提示功能

这篇文章给大家介绍Android开发中怎么实现一个输入框提示功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联建站是专业的珙县网站建设公司,珙县接单;提供成都网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行珙县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

可以使用cursor来动态加载AutoCompleteTextView的数据,从而 实现时时搜索提示,要实现动态加载,只用重写一个类继承于CursorAdapter,然后设定在AutoCompleteTextView上就行了。

Android开发中怎么实现一个输入框提示功能

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开发中怎么实现一个输入框提示功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站名称:Android开发中怎么实现一个输入框提示功能
标题网址:http://sczitong.cn/article/pcghci.html