Android开发中怎么实现一个输入框提示功能-创新互联

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

为凌源等地区用户提供了全套网页设计制作服务,及凌源网站建设行业解决方案。主营业务为做网站、网站建设、凌源网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

可以使用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开发中怎么实现一个输入框提示功能-创新互联
文章出自:https://www.cdcxhl.com/article48/cojihp.html

成都网站建设公司_创新互联,为您提供网站内链手机网站建设做网站企业建站电子商务网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营