亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件

 更新時(shí)間:2016年05月20日 15:48:25   作者:風(fēng)荷舉  
AutoCompleteTextView組件被用在輸入框中能實(shí)現(xiàn)輸入內(nèi)容自動(dòng)補(bǔ)全的功能,類似于大家平時(shí)用Google時(shí)的輸入聯(lián)想,這里我們來(lái)用實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件,特別是實(shí)現(xiàn)郵箱地址補(bǔ)全的例子,非常實(shí)用

AutoCompleteTextView是一個(gè)具有自動(dòng)補(bǔ)全功能的EditView,當(dāng)用戶輸入數(shù)據(jù)后,AutoCompleteTextView就會(huì)將用戶輸入的數(shù)據(jù)與他自己的adapter中的數(shù)據(jù)對(duì)比,如果用戶數(shù)據(jù)與adapter中的某條數(shù)據(jù)的開始部分完全匹配,那么adapter中的這條數(shù)據(jù)就會(huì)出現(xiàn)在下拉提示框中。

其常用屬性定義如下

<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定義了從第幾個(gè)字符開始顯示候補(bǔ)列表。
默認(rèn)值為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設(shè)定的最小值是1,
小于1的情況下,會(huì)默認(rèn)變成1。
 
所以要在不輸入任何字符的條件下顯示候補(bǔ)列表,
就必須重載AutoCompleteTextView這個(gè)控件。
 

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()是判斷輸入文字列長(zhǎng)度是否滿足現(xiàn)實(shí)候補(bǔ)列表的要求的方法。
onFocusChanged()是當(dāng)控件獲得焦點(diǎn)時(shí)讓其顯示候補(bǔ)列表。

2016520154213136.jpg (490×454)

使用AutoCompleteTextView實(shí)現(xiàn)郵箱地址補(bǔ)全
例如:adapter中有3條數(shù)據(jù)“abc”,“hjk”,“abd”,而用戶輸入“ab”,那么下拉提示框中將會(huì)出現(xiàn)“abc”和“abd”。(AutoCompleteTextView默認(rèn)在用戶輸入兩個(gè)字符之后才提示,可以通過setThreshold(1)來(lái)將它設(shè)置為用戶輸入1個(gè)字符后就開始提示)

AutoCompleteTextView在匹配用戶輸入數(shù)據(jù)時(shí),會(huì)調(diào)用performFiltering方法,將用戶數(shù)據(jù)傳入,并調(diào)用adapter的filter來(lái)處理。

因?yàn)楫?dāng)用戶選中下拉列表中的某一項(xiàng)時(shí),AutoCompleteTextView會(huì)使用該項(xiàng)對(duì)應(yīng)的adapter中的數(shù)據(jù)來(lái)填充文本域,這與我們這邊的需求不太相同,因?yàn)槲覀兊腶dapter中只有類似于“@163.com”的email地址后綴,下拉框中的數(shù)據(jù)是我們將用戶輸入和adapter中的數(shù)據(jù)拼接而成的。因此我們需要重寫replaceText方法,以使AutoCompleteTextView來(lái)在用戶選中某一項(xiàng)時(shí),用我們指定的文本來(lái)填充文本域。

然后我們需要為AutoCompleteTextView設(shè)置OnFocusChangeListener來(lái)在用戶移開焦點(diǎn)后,進(jìn)行email地址格式檢查,并且在再次獲得焦點(diǎn)后重啟提示功能。

代碼如下:(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中使用默認(rèn)的emailSufixs中的數(shù)據(jù),可以通過setAdapterString來(lái)更改
    this.setAdapter(new EmailAutoCompleteAdapter(context, R.layout.auto_complete_item, emailSufixs));
    
    //使得在輸入1個(gè)字符之后便開啟自動(dòng)完成
    this.setThreshold(1);

    this.setOnFocusChangeListener(new OnFocusChangeListener() {
      
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
          String text = EmailAutoCompleteTextView.this.getText().toString();
          //當(dāng)該文本域重新獲得焦點(diǎn)后,重啟自動(dòng)完成
          if(!"".equals(text))
            performFiltering(text, 0);
        } else {
          //當(dāng)文本域丟失焦點(diǎn)后,檢查輸入email地址的格式
          EmailAutoCompleteTextView ev = (EmailAutoCompleteTextView) v;
          String text = ev.getText().toString();
          //這里正則寫的有點(diǎn)粗暴:)
          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) {
    //當(dāng)我們?cè)谙吕蛑羞x擇一項(xiàng)時(shí),android會(huì)默認(rèn)使用AutoCompleteTextView中Adapter里的文本來(lái)填充文本域
    //因?yàn)檫@里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) {
    //該方法會(huì)在用戶輸入文本之后調(diào)用,將已輸入的文本與adapter中的數(shù)據(jù)對(duì)比,若它匹配
    //adapter中數(shù)據(jù)的前半部分,那么adapter中的這條數(shù)據(jù)將會(huì)在下拉框中出現(xiàn)
    Log.i(TAG + " performFiltering", text.toString() + "  " + keyCode);
    String t = text.toString();
    
    //因?yàn)橛脩糨斎豚]箱時(shí),都是以字母,數(shù)字開始,而我們的adapter中只會(huì)提供以類似于"@163.com"
    //的郵箱后綴,因此在調(diào)用super.performFiltering時(shí),傳入的一定是以"@"開頭的字符串
    int index = t.indexOf("@");
    if(index == -1) {
      if(t.matches("^[a-zA-Z0-9_]+$")) {
        super.performFiltering("@", keyCode);
      }
      else
        this.dismissDropDown();//當(dāng)用戶中途輸入非法字符時(shí),關(guān)閉下拉提示框
    } 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" />

  <!-- 用于測(cè)試移開焦點(diǎn) -->
  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:drawableLeft="@drawable/amount_selected" />

</LinearLayout>
下拉提示框中每一項(xiàng)(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)

相關(guān)文章

  • Android 全局Dialog的簡(jiǎn)單實(shí)現(xiàn)方法

    Android 全局Dialog的簡(jiǎn)單實(shí)現(xiàn)方法

    本篇文章主要介紹了Android 全局Dialog的簡(jiǎn)單實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-02-02
  • Android判斷網(wǎng)絡(luò)類型的方法(2g,3g還是wifi)

    Android判斷網(wǎng)絡(luò)類型的方法(2g,3g還是wifi)

    這篇文章主要介紹了Android判斷網(wǎng)絡(luò)類型的方法,可實(shí)現(xiàn)判斷2g,3g還是wifi的功能,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)類型的相關(guān)判定技巧,需要的朋友可以參考下
    2016-02-02
  • Android實(shí)現(xiàn)原生鎖屏頁(yè)面音樂控制

    Android實(shí)現(xiàn)原生鎖屏頁(yè)面音樂控制

    這篇文章主要介紹了Android實(shí)現(xiàn)原生鎖屏頁(yè)面音樂控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • android @override 報(bào)錯(cuò)解決方案

    android @override 報(bào)錯(cuò)解決方案

    android @override 報(bào)錯(cuò):就是說(shuō)Java 1.5的編譯器默認(rèn)對(duì)父類的方法進(jìn)行覆蓋,采用@Override進(jìn)行說(shuō)明;但1.6已經(jīng)擴(kuò)展到對(duì)接口的方法;所以如果還是以Java 1.5的編譯器來(lái)編譯的話,會(huì)出現(xiàn)錯(cuò)誤
    2012-12-12
  • Android下拉刷新與輪播圖滑動(dòng)沖突解決方案

    Android下拉刷新與輪播圖滑動(dòng)沖突解決方案

    這篇文章主要介紹了Android實(shí)現(xiàn)下拉刷新與輪播圖沖突解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。隨小編過來(lái)看看吧
    2017-02-02
  • Android Messenger實(shí)現(xiàn)進(jìn)程間雙向通信

    Android Messenger實(shí)現(xiàn)進(jìn)程間雙向通信

    這篇文章主要為大家詳細(xì)介紹了Messenger實(shí)現(xiàn)進(jìn)程間雙向通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android 項(xiàng)目正式簽名打包教程分享

    Android 項(xiàng)目正式簽名打包教程分享

    這篇文章主要介紹了Android 項(xiàng)目正式簽名打包教程分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-03-03
  • RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)

    RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)

    這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Flutter?Widget之NavigationBar使用詳解

    Flutter?Widget之NavigationBar使用詳解

    這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果

    Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評(píng)論