PopupWindow的使用

在安卓中使用PopupWindow类 创建弹出效果对话框

使用示例

    View view = View.inflate(this, R.layout.popup_returns_img, null);
    popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,      ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popupWindow.setTouchable(true);
    popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);       // 设置窗口的位置
//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_background));

        RelativeLayout rl1 = (RelativeLayout) view.findViewById(R.id.rl_camera);
        RelativeLayout rl2 = (RelativeLayout) view.findViewById(R.id.rl_gallary);
        RelativeLayout grayBackground = (RelativeLayout) view.findViewById(R.id.rl_gray_backgroud);
        // 拦截自身触摸事件
        LinearLayout window = (LinearLayout) view.findViewById(R.id.ll_window);
        window.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        // 点击后退按钮,销毁PopupWindow对象
        grayBackground.setFocusable(true);
        grayBackground.setFocusableInTouchMode(true);
        grayBackground.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == event.getKeyCode()){
                    popupWindow.dismiss();
                }
                return false;
            }
        });

//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        View view = View.inflate(this, R.layout.popup_returns_img, null);
        final RadioGroup group = (RadioGroup) view.findViewById(R.id.rgrp_picker);
//
//        builder.setView(view);
//        dialog = builder.create();
//
//        RelativeLayout rl1 = (RelativeLayout) view.findViewById(R.id.rl_camera);
//        RelativeLayout rl2 = (RelativeLayout) view.findViewById(R.id.rl_gallary);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                switch (view.getId()) {
                    case R.id.rl_camera:
                        // 使用相机拍照
                        imageDir = "temp.jpg";
                        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageDir)));

                        startActivityForResult(intent, IMAGE_CAMERA);
                        break;
                    case R.id.rl_gallary:
                        // 从图库中选择图片
//                        intent = new Intent(Intent.ACTION_GET_CONTENT);
//                        intent.setType(IMAGE_UNSPECIFIED);
//                        intent.setPackage("com.google.android.gallery3d");  // 指明包名,直接打开系统自带的图库程序
//                        Intent wrapperIntent = Intent.createChooser(intent, null);
//                        startActivityForResult(wrapperIntent, IMAGE_GALLERY);

                        // 兼容Android 4.4 KITKAT
                        intent = new Intent();
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.setType("image/jpeg");
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);

                            // TODO 在IUNI4.4上 打开图库会挂掉
//                            intent.addCategory(Intent.CATEGORY_DEFAULT);
                            startActivityForResult(intent, IMAGE_GALLERY_KITKAT);
                        } else {
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(intent, IMAGE_GALLERY);
                        }
                        break;
                    case R.id.rl_gray_backgroud:
                        popupWindow.dismiss();
                        break;
                }
            }
        };
        rl1.setOnClickListener(listener);
        rl2.setOnClickListener(listener);
        grayBackground.setOnClickListener(listener);

        popupWindow.showAsDropDown(view);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值