本文实例为大家分享了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 ListmList; 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 下拉列表布局文件
上面自动补全的效果图:
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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助。