当前位置: 移动技术网 > 移动技术>移动开发>Android > 实例讲解Android中的AutoCompleteTextView自动补全组件

实例讲解Android中的AutoCompleteTextView自动补全组件

2019年07月24日  | 移动技术网移动技术  | 我要评论

autocompletetextview是一个具有自动补全功能的editview,当用户输入数据后,autocompletetextview就会将用户输入的数据与他自己的adapter中的数据对比,如果用户数据与adapter中的某条数据的开始部分完全匹配,那么adapter中的这条数据就会出现在下拉提示框中。

其常用属性定义如下

<autocompletetextview
  android:id="@+id/mp002_top_place_input"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:completionthreshold="1"
  android:layout_margintop="5dp" >
</autocompletetextview>

其中android:completionthreshold定义了从第几个字符开始显示候补列表。
默认值为2。

使用例:

autocompletetextview mplace = (autocompletetextview)findviewbyid(r.id.mp002_top_place_input);
arraylist<string> result = new arraylist<string>();
result.add("1111111");
result.add("1222222");
mplace.setadapter(new arrayadapter<string>(
   mp002topactivity.this,
   android.r.layout.simple_dropdown_item_1line,
   result)
);

 
局限性是completionthreshold设定的最小值是1,
小于1的情况下,会默认变成1。
 
所以要在不输入任何字符的条件下显示候补列表,
就必须重载autocompletetextview这个控件。
 

public class myautocompletetextview extends autocompletetextview{
 public myautocompletetextview(context context) {
  super(context);
 }
 public myautocompletetextview(context context, attributeset attrs) {
  super(context, attrs);
 }
 public myautocompletetextview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
 }
 @override
 public boolean enoughtofilter() {
  return true;
 }
 @override
 protected void onfocuschanged(boolean focused, int direction, rect previouslyfocusedrect) {
  super.onfocuschanged(focused, direction, previouslyfocusedrect);
  performfiltering(gettext(), keyevent.keycode_unknown);
 }
}

enoughtofilter()是判断输入文字列长度是否满足现实候补列表的要求的方法。
onfocuschanged()是当控件获得焦点时让其显示候补列表。

2016520154213136.jpg (490×454)

使用autocompletetextview实现邮箱地址补全
例如:adapter中有3条数据“abc”,“hjk”,“abd”,而用户输入“ab”,那么下拉提示框中将会出现“abc”和“abd”。(autocompletetextview默认在用户输入两个字符之后才提示,可以通过setthreshold(1)来将它设置为用户输入1个字符后就开始提示)

autocompletetextview在匹配用户输入数据时,会调用performfiltering方法,将用户数据传入,并调用adapter的filter来处理。

因为当用户选中下拉列表中的某一项时,autocompletetextview会使用该项对应的adapter中的数据来填充文本域,这与我们这边的需求不太相同,因为我们的adapter中只有类似于“@163.com”的email地址后缀,下拉框中的数据是我们将用户输入和adapter中的数据拼接而成的。因此我们需要重写replacetext方法,以使autocompletetextview来在用户选中某一项时,用我们指定的文本来填充文本域。

然后我们需要为autocompletetextview设置onfocuschangelistener来在用户移开焦点后,进行email地址格式检查,并且在再次获得焦点后重启提示功能。

代码如下:(emailautocompletetextview.java)

public class emailautocompletetextview extends autocompletetextview {
  private static final string tag = "emailautocompletetextview";

  private string[] emailsufixs = new string[] { "@163.com",
    "@gmail.com", "@hotmail.com" };

  public emailautocompletetextview(context context) {
    super(context);
    init(context);
  }

  public emailautocompletetextview(context context, attributeset attrs) {
    super(context, attrs);
    init(context);
  }

  public emailautocompletetextview(context context, attributeset attrs,
      int defstyle) {
    super(context, attrs, defstyle);
    init(context);
  }
  
  public void setadapterstring(string[] es) {
    if(es != null && es.length > 0)
      this.emailsufixs = es;
  }
  
  private void init(final context context) {
    //adapter中使用默认的emailsufixs中的数据,可以通过setadapterstring来更改
    this.setadapter(new emailautocompleteadapter(context, r.layout.auto_complete_item, emailsufixs));
    
    //使得在输入1个字符之后便开启自动完成
    this.setthreshold(1);

    this.setonfocuschangelistener(new onfocuschangelistener() {
      
      @override
      public void onfocuschange(view v, boolean hasfocus) {
        if(hasfocus) {
          string text = emailautocompletetextview.this.gettext().tostring();
          //当该文本域重新获得焦点后,重启自动完成
          if(!"".equals(text))
            performfiltering(text, 0);
        } else {
          //当文本域丢失焦点后,检查输入email地址的格式
          emailautocompletetextview ev = (emailautocompletetextview) v;
          string text = ev.gettext().tostring();
          //这里正则写的有点粗暴:)
          if(text != null && text.matches("^[a-za-z0-9_]+@[a-za-z0-9]+\\.[a-za-z0-9]+$")) {
            toast to = new toast(context);
            imageview i = new imageview(context);
            i.setbackgroundresource(r.drawable.img_success);
            to.setview(i);
            to.show();
          } else {
            toast toast = toast.maketext(context, "邮件地址格式不正确", toast.length_short);
            toast.setgravity(gravity.top, 0, 50);
            toast.show();
          }
        }
      }
    });
  }
  
  

  @override
  protected void replacetext(charsequence text) {
    //当我们在下拉框中选择一项时,android会默认使用autocompletetextview中adapter里的文本来填充文本域
    //因为这里adapter中只是存了常用email的后缀
    //因此要重新replace逻辑,将用户输入的部分与后缀合并
    log.i(tag + " replacetext", text.tostring());
    string t = this.gettext().tostring();
    int index = t.indexof("@");
    if(index != -1)
      t = t.substring(0, index);
    super.replacetext(t + text);
  }

  @override
  protected void performfiltering(charsequence text, int keycode) {
    //该方法会在用户输入文本之后调用,将已输入的文本与adapter中的数据对比,若它匹配
    //adapter中数据的前半部分,那么adapter中的这条数据将会在下拉框中出现
    log.i(tag + " performfiltering", text.tostring() + "  " + keycode);
    string t = text.tostring();
    
    //因为用户输入邮箱时,都是以字母,数字开始,而我们的adapter中只会提供以类似于"@163.com"
    //的邮箱后缀,因此在调用super.performfiltering时,传入的一定是以"@"开头的字符串
    int index = t.indexof("@");
    if(index == -1) {
      if(t.matches("^[a-za-z0-9_]+$")) {
        super.performfiltering("@", keycode);
      }
      else
        this.dismissdropdown();//当用户中途输入非法字符时,关闭下拉提示框
    } else {
      super.performfiltering(t.substring(index), keycode);
    }
  }

  
  private class emailautocompleteadapter extends arrayadapter<string> {

    public emailautocompleteadapter(context context, int textviewresourceid, string[] email_s) {
      super(context, textviewresourceid, email_s);
    }

    @override
    public view getview(int position, view convertview, viewgroup parent) {
      log.i(tag, "in getview");
      view v = convertview;
      if (v == null)
        v = layoutinflater.from(getcontext()).inflate(
            r.layout.auto_complete_item, null);
      textview tv = (textview) v.findviewbyid(r.id.tv);
      
      string t = emailautocompletetextview.this.gettext().tostring();
      int index = t.indexof("@");
      if(index != -1)
        t = t.substring(0, index);
      //将用户输入的文本与adapter中的email后缀拼接后,在下拉框中显示
      tv.settext(t + getitem(position));
      log.i(tag, tv.gettext().tostring());
      return v;
    }
  }
}

activity的xml文件如下:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <com.example.testautocompletetextview.emailautocompletetextview
    android:id="@+id/act"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="email address"
    android:textcolor="@color/black" />

  <!-- 用于测试移开焦点 -->
  <edittext
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="20dp"
    android:drawableleft="@drawable/amount_selected" />

</linearlayout>
下拉提示框中每一项(textview)的xml:

<?xml version="1.0" encoding="utf-8"?>
<textview xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tv"
  android:padding="8dp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

提示截图:

2016520154313953.png (477×800)

2016520154329915.png (481×802)2016520154344679.png (481×767)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网