2、编写自定义数据适配器和布局文件,并实现文字变化监听器
3、通过组合方式,实现右侧的删除图标。并根据焦点和文字的变化,动态显示右侧删除图标。@H_419_0@1、通过继承自简单控件autoCompleteTextVIEw实现帐号自动补全@H_419_0@关键代码:
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; } ......省略号...... }@H_419_0@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>@H_419_0@上面自动补全的效果图:@H_419_0@2、通过组合方式实现帐号自动补全复杂控件@H_419_0@关键代码:
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,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初始化未完成,就d窗,而崩溃 mautoComplete.init(mImageVIEw); }}@H_419_0@以上就是本文的全部内容,希望对大家的学习有所帮助。 总结
以上是内存溢出为你收集整理的Android实现登录邮箱的自动补全功能全部内容,希望文章能够帮你解决Android实现登录邮箱的自动补全功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)