Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現登錄郵箱的自動補全功能

Android實現登錄郵箱的自動補全功能

編輯:關於Android編程

本文實例為大家分享了Android登錄郵箱自動補全功能的實現方法,供大家參考,具體內容如下

效果:

實現原理:

1、繼承重寫簡單控件AutoCompleteTextView
2、編寫自定義數據適配器和布局文件,並實現文字變化監聽器
3、通過組合方式,實現右側的刪除圖標。並根據焦點和文字的變化,動態顯示右側刪除圖標。

1、通過繼承自簡單控件AutoCompleteTextView實現帳號自動補全

關鍵代碼:

public class AutoComplete extends AutoCompleteTextView {

  private static final String[] emailSuffix = {
    "@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",
    "@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com",
    "@vip.sina.com"};

  ......省略號......

  //構造函數原型要正確,留給系統調用

  public AutoComplete(Context context) {
    super(context);
    mContext = context;
  }

  public AutoComplete(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
  }

  public void init(ImageView imageView) {
    mImageView = imageView;
    final MyAdatper adapter = new MyAdatper(mContext);
    setAdapter(adapter);
    addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable s) {
        if (isTextWatch) {
          String input = s.toString();

          ......省略號......

          adapter.clearList(); //注意要清空數據,根據輸入的變化,自動生成數據
          if (input.length() > 0) {
            for (int i = 0; i < emailSuffix.length; ++i) {
              adapter.addListData(input + emailSuffix[i]);
            }
          }
          adapter.notifyDataSetChanged();
          showDropDown();//該行代碼會造成崩潰
        }
      }
    });
    //當輸入一個字符的時候就開始檢測
    setThreshold(1);
  }

  private class ViewHolder {
    TextView tv_Text;
  }

  class MyAdatper extends BaseAdapter implements Filterable {
    private List<String> mList;
    private Context mContext;
    private MyFilter mFilter;

    ......省略號......

    public void clearList() {
      mList.clear();
    }

    public void addListData(String strData) {
      mList.add(strData);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view;
      ViewHolder viewHolder;

      if (convertView == null) {
        view = LayoutInflater.from(mContext).inflate(R.layout.activity_autocomplete_item, null);
        viewHolder = new ViewHolder();
        viewHolder.tv_Text = (TextView) view.findViewById(R.id.tv_autocomplete);
        view.setTag(viewHolder);
      } else {
        view = convertView;
        viewHolder = (ViewHolder) view.getTag();
      }

      viewHolder.tv_Text.setText(mList.get(position));

      return view;
    }

    ......省略號......

  }

activity_autocomplete_item 下拉列表布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:background="@color/White"
  android:layout_height="wrap_content">

  <TextView
    android:id="@+id/tv_autocomplete"
    android:padding="15dp"
    android:textSize="20sp"
    android:singleLine="true"
    android:textColor="@color/Black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

上面自動補全的效果圖:

2、通過組合方式實現帳號自動補全復雜控件

關鍵代碼:

public class AdvancedAutoCompleteTextView extends RelativeLayout {
  private Context mContext;
  private AutoComplete mAutoComplete; //上面的自定義控件
  private ImageView mImageView;    //右側的圖標控件

  ......省略號......

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    initViews();
  }
  //代碼方式,初始化布局
  private void initViews() {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    mAutoComplete = new AutoComplete(mContext);
    mAutoComplete.setLayoutParams(params);
    mAutoComplete.setPadding(0, 0, 40, 0);
    mAutoComplete.setSingleLine(true);
    mAutoComplete.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    mAutoComplete.setFitsSystemWindows(true);
    mAutoComplete.setEms(10);
    mAutoComplete.setHint("URS賬號");
    mAutoComplete.setImeOptions(EditorInfo.IME_ACTION_NEXT
                | EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_FLAG_NO_FULLSCREEN);
    mAutoComplete.setDropDownHorizontalOffset(0);
    mAutoComplete.setDropDownVerticalOffset(2);
    mAutoComplete.setBackgroundResource(R.drawable.edit_text_background);

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
    p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    p.addRule(RelativeLayout.CENTER_VERTICAL);
    p.rightMargin = 10;
    mImageView = new ImageView(mContext);
    mImageView.setLayoutParams(p);
    mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    mImageView.setImageResource(R.drawable.unselect);
    mImageView.setClickable(true);
    mImageView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        setText("");
      }
    });

    this.addView(mAutoComplete);
    this.addView(mImageView);
    //監聽獲取焦點事件,目的:輸入帳號時,右側圖標的顯示
    mAutoComplete.setOnFocusChangeListener(new OnFocusChangeListener() {
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus && !mAutoComplete.getText().toString().isEmpty()) {
          mAutoComplete.setShow(false); //如果獲取首次獲取焦點,此時文本不為空,則顯示,並禁止文本改變監聽裡的設置
          mImageView.setImageResource(R.drawable.item_delete);
        } else if (hasFocus) {
          mAutoComplete.setShow(true);//如果獲取首次獲取焦點,此時文本為空,則不改變,並開啟文本改變監聽裡的設置
        } else {
          mAutoComplete.setShow(false);
          mImageView.setImageResource(R.drawable.unselect);
        }
      }
    });

         //對AutoComplete自定義控件初始化,一定要放到最後.否則,會由於AutoComplete初始化未完成,就彈窗,而崩潰

    mAutoComplete.init(mImageView); 
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved