PopupWindow两个具体的使用

2017-02-10 16:41

第一种 : PopupWindows定义

  public class PopupWindows extends PopupWindow
 {

        public PopupWindows(Context mContext, View parent)
        {

            View view = View.inflate(mContext, R.layout.item_popupwindows, null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
            LinearLayout ll_popup = (LinearLayout)view.findViewById(R.id.ll_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

            setWidth(LayoutParams.FILL_PARENT);
            setHeight(LayoutParams.FILL_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            Button bt1 = (Button)view.findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button)view.findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button)view.findViewById(R.id.item_popupwindows_cancel);
            bt1.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    photo();
                    dismiss();
                }
            });
            bt2.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent intent = new Intent(PublishedActivity.this, TestPicActivity.class);
                    startActivity(intent);
                    dismiss();
                }
            });
            bt3.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    dismiss();
                }
            });

        }
    }

使用:

 new PopupWindows(PublishedActivity.this, noScrollgridview);

第二个使用案例:

package com.example.photodemo.utils;

import com.example.photodemo.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

/**
 * 鍥剧墖涓婁紶PopupWindow com.dynamic.PhotoPopupWindows
 *
 */
public class PhotoPopupWindows extends PopupWindow {
    private Context mContext;
    private View mParent;
    private OnClickListener cameraOnclick;
    private OnClickListener chooseOnclick;
    private OnClickListener cancelOnclick;
    private RelativeLayout layout_choose;
    private RelativeLayout layout_camera;
    private RelativeLayout layout_cancel;

    public PhotoPopupWindows(Context context, View parent) {
        mContext = context;
        mParent = parent;
        initView();
    }

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.pop_showavator, null);
        layout_choose = (RelativeLayout) view.findViewById(R.id.layout_choose);
        layout_camera = (RelativeLayout) view.findViewById(R.id.layout_camera);
        layout_cancel = (RelativeLayout) view.findViewById(R.id.layout_cancel);

        setWidth(LayoutParams.MATCH_PARENT);
        setHeight(LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        showAtLocation(mParent, Gravity.BOTTOM, 0, 0);
        //setAnimationStyle(R.style.Animations_GrowFromBottom);
        update();

        layout_camera.setOnClickListener(GetCameraOnclickListener());

        layout_choose.setOnClickListener(GetChooseOnclickListener());

        layout_cancel.setOnClickListener(GetCancelOnclickListener());
    }

    private View.OnClickListener GetCameraOnclickListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (OnCameraOnclick()) {
                    dismiss();
                }
            }
        };
    }

    private boolean OnCameraOnclick() {
        if (cameraOnclick != null) {
            cameraOnclick.onClick(layout_camera);
        }
        return true;
    }

    private View.OnClickListener GetChooseOnclickListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (OnChooseOnclick()) {
                    dismiss();
                }
            }
        };
    }

    private boolean OnChooseOnclick() {
        if (chooseOnclick != null) {
            chooseOnclick.onClick(layout_choose);
        }
        return true;
    }

    private View.OnClickListener GetCancelOnclickListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (OnCancelOnclick()) {
                    dismiss();
                }
            }
        };
    }

    private boolean OnCancelOnclick() {
        if (cancelOnclick != null) {
            cancelOnclick.onClick(layout_cancel);
        }
        return true;
    }

    public void setCameraOnclick(OnClickListener cameraOnclick) {
        this.cameraOnclick = cameraOnclick;
    }

    public void setChooseOnclick(OnClickListener chooseOnclick) {
        this.chooseOnclick = chooseOnclick;
    }

    public void setCancelOnclick(OnClickListener cancelOnclick) {
        this.cancelOnclick = cancelOnclick;
    }

}

使用:
在主要Activity中被调用的

final PhotoPopupWindows popupWindows = new PhotoPopupWindows(mContext, gridview);
        popupWindows.setCameraOnclick(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, temp_uri);
                startActivityForResult(intent, Constant.REQUESTCODE_UPLOADAVATAR_CAMERA);
            }
        });

        popupWindows.setChooseOnclick(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, Constant.REQUESTCODE_UPLOADAVATAR_LOCATION);
            }

        });

FR:海涛高软(QQ技术交流群:386476712)