PopupWindow两个具体的使用

第一种 : 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值