项目设计 中有点击出现如下效果的弹出框显示
使用popupWindow来实现
在页面调用
- PopuWindowUtil util=new PopuWindowUtil(mBaseContext,top_right_btn);
- util.show( R.layout.fullscreen_ui);
布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- >
- <View
- android:id="@+id/left_layout"
- android:layout_width="100dp"
- android:layout_height="fill_parent"
- android:background="#b0000000"
- android:gravity="center"
- />
- <LinearLayout
- android:layout_width="0dip"
- android:layout_height="match_parent"
- android:layout_weight="1.0"
- android:orientation="vertical"
- android:background="@color/white"
- >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/cancel"
- android:text="取消"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:padding="15dip"
- android:clickable="true"
- />
- <TextView
- android:id="@+id/save"
- android:text="确定"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_alignParentRight="true"
- android:padding="15dip"
- android:clickable="true"
- />
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"/>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginTop="10dp"/>
- <RelativeLayout
- android:id="@+id/work_area_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="工作地点"/>
- <TextView
- android:id="@+id/work_area"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"/>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginTop="10dp"/>
- <RelativeLayout
- android:id="@+id/salary_scope_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="月薪范围"/>
- <TextView
- android:id="@+id/salary_scope"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginLeft="10dp"/>
- <RelativeLayout
- android:id="@+id/work_exp_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="工作经验"/>
- <TextView
- android:id="@+id/work_exp"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginLeft="10dp"/>
- <RelativeLayout
- android:id="@+id/degree_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="最低学历"/>
- <TextView
- android:id="@+id/degree"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginLeft="10dp"/>
- <RelativeLayout
- android:id="@+id/work_type_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="工作性质"/>
- <TextView
- android:id="@+id/work_type"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"
- android:layout_marginLeft="10dp"/>
- <RelativeLayout
- android:id="@+id/pub_time_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item_bg"
- android:padding="10dp"
- >
- <TextView style="@style/item_left_style"
- android:text="发布时间"/>
- <TextView
- android:id="@+id/pub_time"
- style="@style/item_right_style"/>
- </RelativeLayout>
- <View style="@style/HorizontalLineDivider"/>
- </LinearLayout>
- </LinearLayout>
PopupWindowUtil工具类
- package alijob.com.util;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Rect;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.util.DisplayMetrics;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.PopupWindow.OnDismissListener;
- /**
- * 弹出框
- *
- * @author jiaqq
- *
- */
- public class PopuWindowUtil {
- private Activity activity;
- private PopupWindow popupWindow;
- private PopupWindow popupWindow1;
- private PopupWindow cityPopupWindow;
- private TextView work_area, salary_scope, work_exp;
- private TextView degree, work_type, pub_time;
- private View view;
- public PopuWindowUtil(Activity activity, View view) {
- super();
- this.activity = activity;
- this.view = view;
- }
- public void show(int layoutid) {
- if (popupWindow == null) {
- final View contentview = LayoutInflater.from(activity).inflate(
- layoutid, null);
- MyOnClickListener clickListener = new MyOnClickListener();
- TextView cancel = (TextView) contentview.findViewById(R.id.cancel);
- TextView save = (TextView) contentview.findViewById(R.id.save);
- cancel.setOnClickListener(clickListener);
- save.setOnClickListener(clickListener);
- contentview.findViewById(R.id.left_layout).setOnClickListener(
- clickListener);
- contentview.findViewById(R.id.work_area_view).setOnClickListener(
- clickListener);
- work_area = (TextView) contentview.findViewById(R.id.work_area);
- salary_scope = (TextView) contentview
- .findViewById(R.id.salary_scope);
- contentview.findViewById(R.id.salary_scope_view)
- .setOnClickListener(clickListener);
- work_exp = (TextView) contentview.findViewById(R.id.work_exp);
- contentview.findViewById(R.id.work_exp_view).setOnClickListener(
- clickListener);
- degree = (TextView) contentview.findViewById(R.id.degree);
- contentview.findViewById(R.id.degree_view).setOnClickListener(
- clickListener);
- work_type = (TextView) contentview.findViewById(R.id.work_type);
- contentview.findViewById(R.id.work_type_view).setOnClickListener(
- clickListener);
- pub_time = (TextView) contentview.findViewById(R.id.pub_time);
- contentview.findViewById(R.id.pub_time_view).setOnClickListener(
- clickListener);
- init(contentview);
- }
- }
- /**
- * 列表弹出选择框
- *
- * @param textView
- * @param nameArray
- * @param valueArray
- * @param title
- * @param item
- */
- public void show(final TextView textView, final String[] nameArray,
- final String[] valueArray, String title, String item) {
- if (popupWindow1 == null) {
- final View contentview = LayoutInflater.from(activity).inflate(
- R.layout.fullscreen_list, null);
- ((TextView) contentview.findViewById(R.id.title)).setText(title);
- final ArrayList<Item> list = new ArrayList<Item>();
- for (int i = 0; i < nameArray.length; i++) {
- list.add(new Item(nameArray[i], valueArray[i]));
- }
- contentview.findViewById(R.id.cancel).setOnClickListener(
- new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- closePopupWindow1();
- }
- });
- ListAdapter adapter = new ListAdapter(activity, list);
- ListView listView = (ListView) contentview.findViewById(R.id.list);
- listView.setAdapter(adapter);
- if (!item.equals("")) {
- for (int i = 0; i < list.size(); i++) {
- if (item.equals(list.get(i).item)) {
- adapter.setIndex(i);
- adapter.notifyDataSetChanged();
- }
- }
- }
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- textView.setText(list.get(position).item);
- if (valueArray != null) {
- textView.setTag(list.get(position).itemId);
- }
- closePopupWindow1();
- }
- });
- init1(contentview);
- }
- }
- public void showCity(final TextView textView) {
- if (cityPopupWindow == null) {
- final View contentview = LayoutInflater.from(activity).inflate(
- R.layout.select_city, null);
- contentview.findViewById(R.id.cancel).setOnClickListener(
- new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- closePopupWindow1();
- }
- });
- List<CityItem> popItems = new ArrayList<CityItem>();
- for (CityItem s : SelectCityActivity.POPULAR_CITIES) {
- popItems.add(s);
- }
- ArrayList<CityItem> mItems = new ArrayList<CityItem>();
- for (int i = 0; i < SelectCityActivity.NAMES.length; i++) {
- mItems.add(new CityItem(SelectCityActivity.NAMES[i],
- SelectCityActivity.PINYINS[i]));
- }
- ContentAdapter adapter = new ContentAdapter(activity, popItems,
- mItems);
- final IndexableListView mListView = (IndexableListView) contentview
- .findViewById(R.id.listview);
- mListView.setAdapter(adapter);
- mListView.setFastScrollEnabled(true);
- mListView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- CityItem ci = (CityItem) mListView.getAdapter().getItem(
- position);
- if (ci.getType() != 1) {
- textView.setText(ci.getName());
- }
- closeCityPopupWindow();
- }
- });
- initCity(contentview);
- }
- }
- class ListAdapter extends BaseAdapter {
- private Context context;
- private ArrayList<Item> list;
- private int index = -1;
- public int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public ListAdapter(Context context, ArrayList<Item> list) {
- super();
- this.context = context;
- this.list = list;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return list.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder;
- if (convertView == null) {
- convertView = LayoutInflater.from(context).inflate(
- R.layout.alertdialog_choice_item, null);
- holder = new ViewHolder();
- holder.item = (TextView) convertView
- .findViewById(R.id.moreaction_dialog_item);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- Item item = list.get(position);
- holder.item.setText(item.item);
- if (position == getIndex()) {
- setDrawableRight(holder.item, R.drawable.selected_img);
- } else {
- holder.item.setCompoundDrawables(null, null, null, null); // 设置右方图标
- }
- return convertView;
- }
- class ViewHolder {
- TextView item;
- }
- }
- class Item {
- public Item(String item, String itemId) {
- super();
- this.item = item;
- this.itemId = itemId;
- }
- String item;
- String itemId;
- }
- public void setDrawableRight(TextView textView, int resid) {
- Drawable leftDrawable = activity.getResources().getDrawable(resid);
- // 调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
- leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(),
- leftDrawable.getMinimumHeight());
- textView.setCompoundDrawables(null, null, leftDrawable, null); // 设置右方图标
- }
- // 工作年限
- public static String workYear[] = { "应届毕业生", "1年以上", "1-3年", "3-5年",
- "5-10年", "10年以上" };
- public static String workYear_value[] = { "2", "3", "4", "5", "6", "7" };
- // 发布时间
- public static String pub_date[] = { "今天", "3天内", "一周内", "一月内" };
- public static String pub_date_value[] = { "1", "3", "7", "30" };
- class MyOnClickListener implements OnClickListener {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.cancel:
- closePopupWindow();
- break;
- case R.id.save:
- closePopupWindow();
- break;
- case R.id.left_layout:
- closePopupWindow();
- break;
- case R.id.work_area_view:
- showCity(work_area);
- break;
- case R.id.salary_scope_view:
- show(salary_scope, BottomView.wish_salary,
- BottomView.wish_salary, "月薪范围", salary_scope.getText()
- .toString());
- break;
- case R.id.work_exp_view:
- show(work_exp, workYear, workYear_value, "工作经验", work_exp
- .getText().toString());
- break;
- case R.id.degree_view:
- show(degree, BottomView.degree, BottomView.degree_value,
- "最低学历", degree.getText().toString());
- break;
- case R.id.work_type_view:
- show(work_type, BottomView.work_type,
- BottomView.work_type_value, "工作性质", work_type.getText()
- .toString());
- break;
- case R.id.pub_time_view:
- show(pub_time, pub_date, pub_date_value, "发布时间", pub_time
- .getText().toString());
- break;
- }
- }
- }
- private void init(View contentview) {
- DisplayMetrics play = activity.getResources().getDisplayMetrics();
- popupWindow = new PopupWindow(contentview, play.widthPixels,
- play.heightPixels);
- popupWindow.setBackgroundDrawable(new BitmapDrawable());
- popupWindow.setAnimationStyle(R.style.popup_horizontal_exit_enter);
- popupWindow.setFocusable(true);
- popupWindow.setClippingEnabled(false);
- popupWindow.setOnDismissListener(new OnDismissListener() {
- public void onDismiss() {
- popupWindow = null;
- }
- });
- Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- int statusBarHeight = frame.top;
- popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,
- statusBarHeight);
- }
- private void init1(View contentview) {
- DisplayMetrics play = activity.getResources().getDisplayMetrics();
- popupWindow1 = new PopupWindow(contentview, play.widthPixels,
- play.heightPixels);
- popupWindow1.setBackgroundDrawable(new BitmapDrawable());
- popupWindow1.setAnimationStyle(R.style.popup_horizontal_exit_enter);
- popupWindow1.setFocusable(true);
- popupWindow1.setClippingEnabled(false);
- popupWindow1.setOnDismissListener(new OnDismissListener() {
- public void onDismiss() {
- popupWindow1 = null;
- }
- });
- Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- int statusBarHeight = frame.top;
- popupWindow1.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,
- statusBarHeight);
- }
- private void initCity(View contentview) {
- DisplayMetrics play = activity.getResources().getDisplayMetrics();
- cityPopupWindow = new PopupWindow(contentview, play.widthPixels,
- play.heightPixels);
- cityPopupWindow.setBackgroundDrawable(new BitmapDrawable());
- cityPopupWindow.setAnimationStyle(R.style.popup_horizontal_exit_enter);
- cityPopupWindow.setFocusable(true);
- cityPopupWindow.setClippingEnabled(false);
- cityPopupWindow.setOnDismissListener(new OnDismissListener() {
- public void onDismiss() {
- cityPopupWindow = null;
- }
- });
- Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- int statusBarHeight = frame.top;
- cityPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0,
- statusBarHeight);
- }
- /**
- * 功能描述:popupwindow消失
- */
- public void closeCityPopupWindow() {
- if (cityPopupWindow != null && cityPopupWindow.isShowing()) {
- cityPopupWindow.dismiss();
- cityPopupWindow = null;
- }
- }
- /**
- * 功能描述:popupwindow消失
- */
- public void closePopupWindow() {
- if (popupWindow != null && popupWindow.isShowing()) {
- popupWindow.dismiss();
- popupWindow = null;
- }
- }
- /**
- * 功能描述:popupwindow消失
- */
- public void closePopupWindow1() {
- if (popupWindow1 != null && popupWindow1.isShowing()) {
- popupWindow1.dismiss();
- popupWindow1 = null;
- }
- }
- }
ps:工具类中还涉及有另一个弹出框的相关方法