PopupWindow工作笔记

本文介绍了一种使用Android的PopupWindow组件实现弹出窗口的方法。通过实例展示了如何创建及展示一个包含多个选项的弹出框,并实现了点击事件处理及自定义布局。此外,还提供了用于选择列表项的弹出框示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目设计 中有点击出现如下效果的弹出框显示


使用popupWindow来实现  

在页面调用

[java]  view plain  copy
 print ?
  1. PopuWindowUtil util=new PopuWindowUtil(mBaseContext,top_right_btn);  
  2.             util.show( R.layout.fullscreen_ui);  

布局文件

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal"   
  6.     >  
  7.      <View   
  8.         android:id="@+id/left_layout"  
  9.         android:layout_width="100dp"  
  10.         android:layout_height="fill_parent"  
  11.         android:background="#b0000000"  
  12.         android:gravity="center"  
  13.         />  
  14.     <LinearLayout   
  15.         android:layout_width="0dip"  
  16.         android:layout_height="match_parent"  
  17.         android:layout_weight="1.0"  
  18.         android:orientation="vertical"  
  19.         android:background="@color/white"  
  20.         >  
  21.         <RelativeLayout   
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="wrap_content"  
  24.             >  
  25.             <TextView   
  26.                 android:id="@+id/cancel"  
  27.                 android:text="取消"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_centerVertical="true"  
  31.                 android:padding="15dip"  
  32.                 android:clickable="true"  
  33.                 />  
  34.             <TextView   
  35.                 android:id="@+id/save"  
  36.                 android:text="确定"  
  37.                 android:layout_width="wrap_content"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:layout_centerVertical="true"  
  40.                 android:layout_alignParentRight="true"  
  41.                 android:padding="15dip"  
  42.                 android:clickable="true"  
  43.                 />  
  44.         </RelativeLayout>  
  45.         <View style="@style/HorizontalLineDivider"/>  
  46.         <View style="@style/HorizontalLineDivider"  
  47.             android:layout_marginTop="10dp"/>  
  48.        <RelativeLayout   
  49.            android:id="@+id/work_area_view"  
  50.            android:layout_width="match_parent"  
  51.            android:layout_height="wrap_content"  
  52.            android:background="@drawable/item_bg"  
  53.            android:padding="10dp"  
  54.            >  
  55.            <TextView style="@style/item_left_style"  
  56.                android:text="工作地点"/>  
  57.            <TextView   
  58.                android:id="@+id/work_area"  
  59.                style="@style/item_right_style"/>  
  60.        </RelativeLayout>  
  61.        <View style="@style/HorizontalLineDivider"/>  
  62.         <View style="@style/HorizontalLineDivider"  
  63.             android:layout_marginTop="10dp"/>  
  64.         <RelativeLayout   
  65.             android:id="@+id/salary_scope_view"  
  66.            android:layout_width="match_parent"  
  67.            android:layout_height="wrap_content"  
  68.            android:background="@drawable/item_bg"  
  69.            android:padding="10dp"  
  70.            >  
  71.            <TextView style="@style/item_left_style"  
  72.                android:text="月薪范围"/>  
  73.            <TextView  
  74.                android:id="@+id/salary_scope"  
  75.                 style="@style/item_right_style"/>  
  76.        </RelativeLayout>  
  77.        <View style="@style/HorizontalLineDivider"  
  78.            android:layout_marginLeft="10dp"/>  
  79.        <RelativeLayout   
  80.            android:id="@+id/work_exp_view"  
  81.            android:layout_width="match_parent"  
  82.            android:layout_height="wrap_content"  
  83.            android:background="@drawable/item_bg"  
  84.            android:padding="10dp"  
  85.            >  
  86.            <TextView style="@style/item_left_style"  
  87.                android:text="工作经验"/>  
  88.            <TextView   
  89.                android:id="@+id/work_exp"  
  90.                style="@style/item_right_style"/>  
  91.        </RelativeLayout>  
  92.        <View style="@style/HorizontalLineDivider"  
  93.            android:layout_marginLeft="10dp"/>  
  94.        <RelativeLayout   
  95.            android:id="@+id/degree_view"  
  96.            android:layout_width="match_parent"  
  97.            android:layout_height="wrap_content"  
  98.            android:background="@drawable/item_bg"  
  99.            android:padding="10dp"  
  100.            >  
  101.            <TextView style="@style/item_left_style"  
  102.                android:text="最低学历"/>  
  103.            <TextView   
  104.                android:id="@+id/degree"  
  105.                style="@style/item_right_style"/>  
  106.        </RelativeLayout>  
  107.        <View style="@style/HorizontalLineDivider"  
  108.            android:layout_marginLeft="10dp"/>  
  109.        <RelativeLayout   
  110.            android:id="@+id/work_type_view"  
  111.            android:layout_width="match_parent"  
  112.            android:layout_height="wrap_content"  
  113.            android:background="@drawable/item_bg"  
  114.            android:padding="10dp"  
  115.            >  
  116.            <TextView style="@style/item_left_style"  
  117.                android:text="工作性质"/>  
  118.            <TextView   
  119.                android:id="@+id/work_type"  
  120.                style="@style/item_right_style"/>  
  121.        </RelativeLayout>  
  122.        <View style="@style/HorizontalLineDivider"  
  123.            android:layout_marginLeft="10dp"/>  
  124.        <RelativeLayout   
  125.            android:id="@+id/pub_time_view"  
  126.            android:layout_width="match_parent"  
  127.            android:layout_height="wrap_content"  
  128.            android:background="@drawable/item_bg"  
  129.            android:padding="10dp"  
  130.            >  
  131.            <TextView style="@style/item_left_style"  
  132.                android:text="发布时间"/>  
  133.            <TextView   
  134.                android:id="@+id/pub_time"  
  135.                style="@style/item_right_style"/>  
  136.        </RelativeLayout>  
  137.        <View style="@style/HorizontalLineDivider"/>  
  138.     </LinearLayout>  
  139. </LinearLayout>  

PopupWindowUtil工具类

[java]  view plain  copy
 print ?
  1. package alijob.com.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6.   
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.graphics.Rect;  
  10. import android.graphics.drawable.BitmapDrawable;  
  11. import android.graphics.drawable.Drawable;  
  12. import android.util.DisplayMetrics;  
  13. import android.view.Gravity;  
  14. import android.view.LayoutInflater;  
  15. import android.view.View;  
  16. import android.view.ViewGroup;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.AdapterView;  
  19. import android.widget.BaseAdapter;  
  20. import android.widget.ListView;  
  21. import android.widget.PopupWindow;  
  22. import android.widget.TextView;  
  23. import android.widget.AdapterView.OnItemClickListener;  
  24. import android.widget.PopupWindow.OnDismissListener;  
  25.   
  26. /** 
  27.  * 弹出框 
  28.  *  
  29.  * @author jiaqq 
  30.  *  
  31.  */  
  32. public class PopuWindowUtil {  
  33.     private Activity activity;  
  34.     private PopupWindow popupWindow;  
  35.     private PopupWindow popupWindow1;  
  36.     private PopupWindow cityPopupWindow;  
  37.     private TextView work_area, salary_scope, work_exp;  
  38.     private TextView degree, work_type, pub_time;  
  39.     private View view;  
  40.   
  41.     public PopuWindowUtil(Activity activity, View view) {  
  42.         super();  
  43.         this.activity = activity;  
  44.         this.view = view;  
  45.     }  
  46.   
  47.     public void show(int layoutid) {  
  48.         if (popupWindow == null) {  
  49.             final View contentview = LayoutInflater.from(activity).inflate(  
  50.                     layoutid, null);  
  51.             MyOnClickListener clickListener = new MyOnClickListener();  
  52.             TextView cancel = (TextView) contentview.findViewById(R.id.cancel);  
  53.             TextView save = (TextView) contentview.findViewById(R.id.save);  
  54.             cancel.setOnClickListener(clickListener);  
  55.             save.setOnClickListener(clickListener);  
  56.             contentview.findViewById(R.id.left_layout).setOnClickListener(  
  57.                     clickListener);  
  58.             contentview.findViewById(R.id.work_area_view).setOnClickListener(  
  59.                     clickListener);  
  60.             work_area = (TextView) contentview.findViewById(R.id.work_area);  
  61.             salary_scope = (TextView) contentview  
  62.                     .findViewById(R.id.salary_scope);  
  63.             contentview.findViewById(R.id.salary_scope_view)  
  64.                     .setOnClickListener(clickListener);  
  65.             work_exp = (TextView) contentview.findViewById(R.id.work_exp);  
  66.             contentview.findViewById(R.id.work_exp_view).setOnClickListener(  
  67.                     clickListener);  
  68.             degree = (TextView) contentview.findViewById(R.id.degree);  
  69.             contentview.findViewById(R.id.degree_view).setOnClickListener(  
  70.                     clickListener);  
  71.             work_type = (TextView) contentview.findViewById(R.id.work_type);  
  72.             contentview.findViewById(R.id.work_type_view).setOnClickListener(  
  73.                     clickListener);  
  74.             pub_time = (TextView) contentview.findViewById(R.id.pub_time);  
  75.             contentview.findViewById(R.id.pub_time_view).setOnClickListener(  
  76.                     clickListener);  
  77.             init(contentview);  
  78.         }  
  79.     }  
  80.   
  81.     /** 
  82.      * 列表弹出选择框 
  83.      *  
  84.      * @param textView 
  85.      * @param nameArray 
  86.      * @param valueArray 
  87.      * @param title 
  88.      * @param item 
  89.      */  
  90.     public void show(final TextView textView, final String[] nameArray,  
  91.             final String[] valueArray, String title, String item) {  
  92.         if (popupWindow1 == null) {  
  93.             final View contentview = LayoutInflater.from(activity).inflate(  
  94.                     R.layout.fullscreen_list, null);  
  95.             ((TextView) contentview.findViewById(R.id.title)).setText(title);  
  96.             final ArrayList<Item> list = new ArrayList<Item>();  
  97.             for (int i = 0; i < nameArray.length; i++) {  
  98.                 list.add(new Item(nameArray[i], valueArray[i]));  
  99.             }  
  100.             contentview.findViewById(R.id.cancel).setOnClickListener(  
  101.                     new OnClickListener() {  
  102.   
  103.                         @Override  
  104.                         public void onClick(View v) {  
  105.                             // TODO Auto-generated method stub  
  106.                             closePopupWindow1();  
  107.                         }  
  108.                     });  
  109.             ListAdapter adapter = new ListAdapter(activity, list);  
  110.             ListView listView = (ListView) contentview.findViewById(R.id.list);  
  111.             listView.setAdapter(adapter);  
  112.             if (!item.equals("")) {  
  113.                 for (int i = 0; i < list.size(); i++) {  
  114.                     if (item.equals(list.get(i).item)) {  
  115.                         adapter.setIndex(i);  
  116.                         adapter.notifyDataSetChanged();  
  117.                     }  
  118.                 }  
  119.             }  
  120.             listView.setOnItemClickListener(new OnItemClickListener() {  
  121.                 @Override  
  122.                 public void onItemClick(AdapterView<?> parent, View view,  
  123.                         int position, long id) {  
  124.                     textView.setText(list.get(position).item);  
  125.                     if (valueArray != null) {  
  126.                         textView.setTag(list.get(position).itemId);  
  127.                     }  
  128.                     closePopupWindow1();  
  129.                 }  
  130.             });  
  131.             init1(contentview);  
  132.         }  
  133.     }  
  134.   
  135.     public void showCity(final TextView textView) {  
  136.         if (cityPopupWindow == null) {  
  137.             final View contentview = LayoutInflater.from(activity).inflate(  
  138.                     R.layout.select_city, null);  
  139.             contentview.findViewById(R.id.cancel).setOnClickListener(  
  140.                     new OnClickListener() {  
  141.   
  142.                         @Override  
  143.                         public void onClick(View v) {  
  144.                             // TODO Auto-generated method stub  
  145.                             closePopupWindow1();  
  146.                         }  
  147.                     });  
  148.             List<CityItem> popItems = new ArrayList<CityItem>();  
  149.             for (CityItem s : SelectCityActivity.POPULAR_CITIES) {  
  150.                 popItems.add(s);  
  151.             }  
  152.             ArrayList<CityItem> mItems = new ArrayList<CityItem>();  
  153.             for (int i = 0; i < SelectCityActivity.NAMES.length; i++) {  
  154.                 mItems.add(new CityItem(SelectCityActivity.NAMES[i],  
  155.                         SelectCityActivity.PINYINS[i]));  
  156.             }  
  157.   
  158.             ContentAdapter adapter = new ContentAdapter(activity, popItems,  
  159.                     mItems);  
  160.             final IndexableListView mListView = (IndexableListView) contentview  
  161.                     .findViewById(R.id.listview);  
  162.             mListView.setAdapter(adapter);  
  163.             mListView.setFastScrollEnabled(true);  
  164.             mListView.setOnItemClickListener(new OnItemClickListener() {  
  165.                 @Override  
  166.                 public void onItemClick(AdapterView<?> parent, View view,  
  167.                         int position, long id) {  
  168.                     CityItem ci = (CityItem) mListView.getAdapter().getItem(  
  169.                             position);  
  170.                     if (ci.getType() != 1) {  
  171.                         textView.setText(ci.getName());  
  172.                     }  
  173.                     closeCityPopupWindow();  
  174.                 }  
  175.             });  
  176.             initCity(contentview);  
  177.         }  
  178.     }  
  179.   
  180.     class ListAdapter extends BaseAdapter {  
  181.         private Context context;  
  182.         private ArrayList<Item> list;  
  183.         private int index = -1;  
  184.   
  185.         public int getIndex() {  
  186.             return index;  
  187.         }  
  188.   
  189.         public void setIndex(int index) {  
  190.             this.index = index;  
  191.         }  
  192.   
  193.         public ListAdapter(Context context, ArrayList<Item> list) {  
  194.             super();  
  195.             this.context = context;  
  196.             this.list = list;  
  197.         }  
  198.   
  199.         @Override  
  200.         public int getCount() {  
  201.             // TODO Auto-generated method stub  
  202.             return list.size();  
  203.         }  
  204.   
  205.         @Override  
  206.         public Object getItem(int position) {  
  207.             // TODO Auto-generated method stub  
  208.             return list.get(position);  
  209.         }  
  210.   
  211.         @Override  
  212.         public long getItemId(int position) {  
  213.             // TODO Auto-generated method stub  
  214.             return position;  
  215.         }  
  216.   
  217.         @Override  
  218.         public View getView(int position, View convertView, ViewGroup parent) {  
  219.             // TODO Auto-generated method stub  
  220.             ViewHolder holder;  
  221.             if (convertView == null) {  
  222.                 convertView = LayoutInflater.from(context).inflate(  
  223.                         R.layout.alertdialog_choice_item, null);  
  224.                 holder = new ViewHolder();  
  225.                 holder.item = (TextView) convertView  
  226.                         .findViewById(R.id.moreaction_dialog_item);  
  227.                 convertView.setTag(holder);  
  228.             } else {  
  229.                 holder = (ViewHolder) convertView.getTag();  
  230.             }  
  231.             Item item = list.get(position);  
  232.             holder.item.setText(item.item);  
  233.             if (position == getIndex()) {  
  234.                 setDrawableRight(holder.item, R.drawable.selected_img);  
  235.             } else {  
  236.                 holder.item.setCompoundDrawables(nullnullnullnull); // 设置右方图标  
  237.             }  
  238.             return convertView;  
  239.         }  
  240.   
  241.         class ViewHolder {  
  242.             TextView item;  
  243.         }  
  244.     }  
  245.   
  246.     class Item {  
  247.   
  248.         public Item(String item, String itemId) {  
  249.             super();  
  250.             this.item = item;  
  251.             this.itemId = itemId;  
  252.         }  
  253.   
  254.         String item;  
  255.         String itemId;  
  256.     }  
  257.   
  258.     public void setDrawableRight(TextView textView, int resid) {  
  259.         Drawable leftDrawable = activity.getResources().getDrawable(resid);  
  260.         // 调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示  
  261.         leftDrawable.setBounds(00, leftDrawable.getMinimumWidth(),  
  262.                 leftDrawable.getMinimumHeight());  
  263.         textView.setCompoundDrawables(nullnull, leftDrawable, null); // 设置右方图标  
  264.     }  
  265.   
  266.     // 工作年限  
  267.     public static String workYear[] = { "应届毕业生""1年以上""1-3年""3-5年",  
  268.             "5-10年""10年以上" };  
  269.     public static String workYear_value[] = { "2""3""4""5""6""7" };  
  270.     // 发布时间  
  271.     public static String pub_date[] = { "今天""3天内""一周内""一月内" };  
  272.     public static String pub_date_value[] = { "1""3""7""30" };  
  273.   
  274.     class MyOnClickListener implements OnClickListener {  
  275.   
  276.         @Override  
  277.         public void onClick(View v) {  
  278.             // TODO Auto-generated method stub  
  279.             switch (v.getId()) {  
  280.             case R.id.cancel:  
  281.                 closePopupWindow();  
  282.                 break;  
  283.             case R.id.save:  
  284.                 closePopupWindow();  
  285.                 break;  
  286.             case R.id.left_layout:  
  287.                 closePopupWindow();  
  288.                 break;  
  289.             case R.id.work_area_view:  
  290.                 showCity(work_area);  
  291.                 break;  
  292.             case R.id.salary_scope_view:  
  293.                 show(salary_scope, BottomView.wish_salary,  
  294.                         BottomView.wish_salary, "月薪范围", salary_scope.getText()  
  295.                                 .toString());  
  296.                 break;  
  297.             case R.id.work_exp_view:  
  298.                 show(work_exp, workYear, workYear_value, "工作经验", work_exp  
  299.                         .getText().toString());  
  300.                 break;  
  301.             case R.id.degree_view:  
  302.                 show(degree, BottomView.degree, BottomView.degree_value,  
  303.                         "最低学历", degree.getText().toString());  
  304.                 break;  
  305.             case R.id.work_type_view:  
  306.                 show(work_type, BottomView.work_type,  
  307.                         BottomView.work_type_value, "工作性质", work_type.getText()  
  308.                                 .toString());  
  309.                 break;  
  310.             case R.id.pub_time_view:  
  311.                 show(pub_time, pub_date, pub_date_value, "发布时间", pub_time  
  312.                         .getText().toString());  
  313.                 break;  
  314.             }  
  315.         }  
  316.   
  317.     }  
  318.   
  319.     private void init(View contentview) {  
  320.         DisplayMetrics play = activity.getResources().getDisplayMetrics();  
  321.         popupWindow = new PopupWindow(contentview, play.widthPixels,  
  322.                 play.heightPixels);  
  323.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  324.         popupWindow.setAnimationStyle(R.style.popup_horizontal_exit_enter);  
  325.         popupWindow.setFocusable(true);  
  326.         popupWindow.setClippingEnabled(false);  
  327.         popupWindow.setOnDismissListener(new OnDismissListener() {  
  328.             public void onDismiss() {  
  329.                 popupWindow = null;  
  330.             }  
  331.         });  
  332.         Rect frame = new Rect();  
  333.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  334.         int statusBarHeight = frame.top;  
  335.         popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,  
  336.                 statusBarHeight);  
  337.     }  
  338.   
  339.     private void init1(View contentview) {  
  340.         DisplayMetrics play = activity.getResources().getDisplayMetrics();  
  341.         popupWindow1 = new PopupWindow(contentview, play.widthPixels,  
  342.                 play.heightPixels);  
  343.         popupWindow1.setBackgroundDrawable(new BitmapDrawable());  
  344.         popupWindow1.setAnimationStyle(R.style.popup_horizontal_exit_enter);  
  345.         popupWindow1.setFocusable(true);  
  346.         popupWindow1.setClippingEnabled(false);  
  347.         popupWindow1.setOnDismissListener(new OnDismissListener() {  
  348.             public void onDismiss() {  
  349.                 popupWindow1 = null;  
  350.             }  
  351.         });  
  352.         Rect frame = new Rect();  
  353.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  354.         int statusBarHeight = frame.top;  
  355.         popupWindow1.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,  
  356.                 statusBarHeight);  
  357.     }  
  358.   
  359.     private void initCity(View contentview) {  
  360.         DisplayMetrics play = activity.getResources().getDisplayMetrics();  
  361.         cityPopupWindow = new PopupWindow(contentview, play.widthPixels,  
  362.                 play.heightPixels);  
  363.         cityPopupWindow.setBackgroundDrawable(new BitmapDrawable());  
  364.         cityPopupWindow.setAnimationStyle(R.style.popup_horizontal_exit_enter);  
  365.         cityPopupWindow.setFocusable(true);  
  366.         cityPopupWindow.setClippingEnabled(false);  
  367.         cityPopupWindow.setOnDismissListener(new OnDismissListener() {  
  368.             public void onDismiss() {  
  369.                 cityPopupWindow = null;  
  370.             }  
  371.         });  
  372.         Rect frame = new Rect();  
  373.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  374.         int statusBarHeight = frame.top;  
  375.         cityPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,  
  376.                 statusBarHeight);  
  377.     }  
  378.   
  379.     /** 
  380.      * 功能描述:popupwindow消失 
  381.      */  
  382.     public void closeCityPopupWindow() {  
  383.         if (cityPopupWindow != null && cityPopupWindow.isShowing()) {  
  384.             cityPopupWindow.dismiss();  
  385.             cityPopupWindow = null;  
  386.         }  
  387.     }  
  388.   
  389.     /** 
  390.      * 功能描述:popupwindow消失 
  391.      */  
  392.     public void closePopupWindow() {  
  393.         if (popupWindow != null && popupWindow.isShowing()) {  
  394.             popupWindow.dismiss();  
  395.             popupWindow = null;  
  396.         }  
  397.     }  
  398.   
  399.     /** 
  400.      * 功能描述:popupwindow消失 
  401.      */  
  402.     public void closePopupWindow1() {  
  403.         if (popupWindow1 != null && popupWindow1.isShowing()) {  
  404.             popupWindow1.dismiss();  
  405.             popupWindow1 = null;  
  406.         }  
  407.     }  
  408. }  

ps:工具类中还涉及有另一个弹出框的相关方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值