PopupWindow (zhong pin)

import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams; import com.android.archermind.activity.R; public class PopupWindowHelper { private Context context; // 当前所在Activity的布局ID private int layout; // 当前所在Activity的监听类(OnClickListener) private OnClickListener listener; private PopupWindow pw; // 当前所在Activity的最外层布局组件的ID private int view; // PopupWindow所在的y轴坐标 private int y; // PopupWindow的整体高度 private int height; // PopupWindow中的数据(绘制每一项) private String[][] datas; // PopupWindow中的分割线的数目 private int lineCount; // 设置TextView的ID(可以设置初始值) private int id = 0; public PopupWindowHelper(Context context, int layout, OnClickListener listener, int view, int y, int height, String[][] datas, int lineCount, int id){ this.context = context; this.layout = layout; this.listener = listener; this.view = view; this.y = y; this.height = height; this.datas = datas; this.lineCount = lineCount; this.id = id; } /** * 弹出菜单窗口 */ public PopupWindow createPopupWindow() { LinearLayout pwLayout = new LinearLayout(this.context); for(int i = 0; i < datas.length; i++) { String[] text = datas[i]; if(i < lineCount) { initTextViewLayoutForPopup(text, pwLayout, true); } else { initTextViewLayoutForPopup(text, pwLayout, false); } } String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater); LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(this.layout, null); if(pw == null) { pw = new PopupWindow(pwLayout); pw.showAtLocation(linearLayout.findViewById(this.view), Gravity.CENTER,0,0); } else if(!pw.isShowing()) { pw.showAtLocation(linearLayout.findViewById(this.view), Gravity.CENTER,0,0); } pw.update(-101, this.y, 161, this.height); return pw; } /** * 动态加载PopupWindow的界面(用于MENU) * @param texts * @param pwLayout * @param isNeedLine */ private void initTextViewLayoutForPopup(String[] texts, LinearLayout pwLayout, Boolean isNeedLine) { pwLayout.setOrientation(LinearLayout.VERTICAL); pwLayout.setBackgroundResource(R.drawable.popup_bg); for(int i = 0; i < texts.length; i++, id++){ TextView textView = new TextView(context); textView.setTextColor(Color.parseColor("#FFFFFF")); textView.setText(texts[i]); textView.setTypeface(null, Typeface.BOLD); textView.setPadding(10, 4, 0, 4); textView.setId(id); textView.setOnClickListener(this.listener); pwLayout.addView(textView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } if(isNeedLine) { ImageView line = new ImageView(context); line.setImageResource(R.drawable.pop_line); line.setPadding(5, 5, 0, 0); pwLayout.addView(line, new LinearLayout.LayoutParams(135, LayoutParams.WRAP_CONTENT)); } } }

输入监听:

EditText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { showInstantResultList(); }

更多相关文章

  1. android:布局参数,控件属性及各种xml的作用
  2. Android布局案例之人人android九宫格
  3. Android中布局的巧妙设计【android进化二十六】
  4. Android之LinearLayout线性布局
  5. Android的View体系(三):View坐标以及方法说明
  6. 深入探索Android布局优化(下)
  7. 布局与样式
  8. Android 学习笔记--android――listview的多种行布局的复用

随机推荐

  1. 华为防火墙与思科路由器IPsec对接参数
  2. laket-admin 是一套使用 thinkphp6 和 la
  3. 插入排序和归并排序
  4. 2021年DevOps的四大趋势
  5. always on 清理日志记录
  6. 聚类的基本介绍
  7. 企业如何建立一体化数据分析平台?还是得说
  8. AWS 大数据实战 Lab2 - 批量数据处理(三)
  9. 深度学习框架浅析
  10. 苹果Mac全自动时间跟踪器:Qbserve