使用PopupWindow配合EditText制作模糊搜索
来源:互联网 发布:天狼星期货软件 编辑:程序博客网 时间:2024/04/30 09:36
代码位于Activity中
包括下面的内部内,也可以选择不使用内部类
private PopupWindow popupwindow;//要弹出的PopupWindow private ListView popup_listview;//PopupWindow里面的listView private int site_i = 1;//记录ListView是否setadapter private View PopView = null;//布局注入器/** * 下拉选项弹出列表 * * 模拟模糊搜索功能 * * @param control * @param adapter */ private void PopSite(final TextView control, BaseAdapter adapter, int i, ListView listview) { if (PopView == null) { // this.popup_listview = listview; // 使用布局注入器注入布局 PopView = getLayoutInflater() .inflate(R.layout.popup_listview, null); // 先初始化listview以便于使用动态设定高度 popup_listview = (ListView) PopView .findViewById(R.id.popup_listview); // 设置PopupWindow的宽高,高度自适应, //宽度和调用这个方法的控件一样宽可以自己调整 popupwindow = new PopupWindow(PopView, control.getWidth(), LayoutParams.WRAP_CONTENT, true); // 解决弹出popup输入法被遮挡 popupwindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 焦点设置,点击区域外消失参数必须为true // 如果是配合EditText进行搜索提示或者是模糊搜索需要改成false //本人小米1测试的时候点击listView无效, //但是拿夜神模拟器和红米2的时候无冲突点击有效果 popupwindow.setFocusable(false); // 点击区域外消失必须设置此方法 //由于失去了焦点此方法无效 popupwindow.setBackgroundDrawable(new BitmapDrawable()); // 选择在某一个控件下面 popupwindow.showAsDropDown(control); } else { adapter.notifyDataSetChanged(); } adapter.notifyDataSetChanged(); // 判断是否是第一次进来如果是直接setadapter,如果不是第一次刷新数据 if (i <= 1) { popup_listview.setAdapter(adapter); i++; } else { adapter.notifyDataSetChanged(); } // 下拉列表item点击事件 popup_listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//设置值获得焦点 popupwindow.setFocusable(true);//我这里没有直接获取item里面控件的字符串直接使用的集合里面的//根据不同的控件所需要的值不同,然后进行赋值 "\t"是空格 if (control == site) { site.setText(site_list.get(position).getName() .toString() + "\t" + site_list.get(position).getName().toString());//点击item后将相对应的数据放入EditText中,本人是直接存在集合里面拿取的/* 获取到长度将光标放在最后面 当拿到数据之后将光标移动到字符串的最后面这里我没有直接使用control.setSelection(control.getText().length());是应为我弄的是TextView,TextView和EditText都要显示所以没有直接使用EditText有需要的可以直接修改成EditText然后在最后面写上就可以了*/site.setSelection(control.getText().length()); } if (control == et_exist) { staffid = exist_list.get(position).getStaffId(); et_exist.setText(exist_list.get(position).getName() .toString() + "\t" + exist_list.get(position).getPhone().toString()); // 获取到长度将光标放在最后面 et_exist.setSelection(control.getText().length()); } // 点击关闭popup if (null != popupwindow && popupwindow.isShowing()) { popupwindow.dismiss(); PopView = null; } } }); }
下面是site的适配器
有需要的自己进行修改适配器
public class SiteAdapter extends BaseAdapter { private List<TElectricBoxDto> datalist; private Context Context; private ViewHolder holder; public SiteAdapter(Context Context, List<TElectricBoxDto> datalist) { this.Context = Context; this.datalist = datalist; } /** 根据item的长度设置listView的高度 需要在setadapter以后调用 建议先调用setData再调用此方法*/ public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } public void setData(List<TElectricBoxDto> datalist) { this.datalist = datalist; } @Override public int getCount() { if (datalist == null) { return 0; } return datalist.size(); } @Override public Object getItem(int position) { return datalist.get(position); } @Override public long getItemId(int position) { return position; } /** * 下面是重要代码 */ @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater _LayoutInflater = LayoutInflater.from(Context); holder = null; if (convertView == null) { convertView = _LayoutInflater.inflate(R.layout.binding_site_item, null); holder = new ViewHolder(); holder.site = (TextView) convertView.findViewById(R.id.site_site); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.site.setText(内容); return convertView; } class ViewHolder { TextView site;// 地址 }}
本人使用了内部类来监听EditText的输入事件
调用方法直接使用
site = (EditText) findViewById(R.id.binding_site);
site.addTextChangedListener(new NewTextWatcher(site));
由于本人要使用多个EditText以及要实现多个地方所以用了此方法,也可以直接使用site.addTextChangedListener(this);看你们喜欢哪一种
把多余的删掉就好了
// EditText class NewTextWatcher implements TextWatcher { private EditText editText; public NewTextWatcher(EditText editText) { this.editText = editText; } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // 已存在地址信息 if (editText == site) { if (s.length() == 0) { if (null != popupwindow && popupwindow.isShowing()) { //需要滞空的在这里写 number.setText(""); company.setText(""); phone.setText(""); site_adapter.setData(null); site_adapter.notifyDataSetChanged(); popupwindow.dismiss(); PopView = null; site_i = 1; } } else { PopSite(site, site_adapter, site_i, popup_listview); String searchKey = site.getText().toString(); if (!StringUtilsExt.isEmpty(searchKey)) { SiteData(searchKey, site); } site_adapter .setListViewHeightBasedOnChildren(popup_listview); } } // 已存在帐号信息 if (editText == et_exist) { if (s.length() == 0) { if (null != popupwindow && popupwindow.isShowing()) { //赋值为空,刷新适配器并关闭popup exist_adapter.setData(null); exist_adapter.notifyDataSetChanged(); popupwindow.dismiss(); PopView = null; site_i = 1; } } else { PopSite(et_exist, exist_adapter, site_i, popup_listview); String searchKey = et_exist.getText().toString(); if (!StringUtilsExt.isEmpty(searchKey)) { SiteData(searchKey, et_exist); exist_adapter .setListViewHeightBasedOnChildren(popup_listview); } } } } }
本人还是一个菜鸟有很多地方写的不够好,不够节俭,代码有点乱,如果有建议的可以留下来,谢谢各位大神了!!!
0 0
- 使用PopupWindow配合EditText制作模糊搜索
- DatePickerDialog和EditText配合使用
- EditText、ImageSpan和SpannableString的配合使用
- PopupWindow、EditText
- 模糊搜索框 EditText动态过滤Listview并改变颜色(仿微信模糊搜索)
- 谓词的使用:模糊搜索
- 有EditText的activity和输入法的配合使用
- PopupWindow中使用EditText软键盘的弹出和隐藏
- PopupWindow中使用EditText软键盘的弹出和隐藏
- JDBC传入参数使用LIKE模糊搜索
- 模糊搜索
- 模糊搜索
- EditText实现搜索框常见需求详解(配合软键盘的应用和模拟搜索List结果集)
- 使用bootstrap制作搜索框
- 制作搜索页面,使用TextWatcher
- 使用ScrollView制作Title渐变,配合RecyclerView展示列表
- TableViewController中的搜索框和mj刷新配合使用
- android EditText 与 ImageView配合
- 将Mercurial整合到Flask项目中
- 自定义View之路——Canvas(save,restore,translate,rotate)
- removeCallbacksAndMessages(null)的含义
- mysql 模拟rownum 并做一些简单适用的操作
- 算法导论第十五章-动态规划-Cpp代码实现
- 使用PopupWindow配合EditText制作模糊搜索
- 问题咨询
- 简述HLS,HTTP,RTSP,RTMP协议的区别
- Handler消息传递机制
- 485 繁琐问题,烦烦烦
- ndroid studio修改项目名,模块名,包名
- Android中使用ImageView控件显示网络图片
- JZOJ.1166 树中点对距离
- CocoaPods相关问题总结