接口回调实现popwindow展示

常用的popwindow类,通过接口调用,下面是工具类的部分

public class AddPictureWindow implements OnClickListener {
    private PopupWindow mPop;// 窗口
    private Context mContext;// 上下文
    private TextView tvPhoto, tvAlbum, tvCancel;// 拍照,相册, 取消

    public AddPictureWindow(Context mContext) {
        this.mContext = mContext;
        initView();

    }

    /**
     * 初始化组件
     */
    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_add_picture, null);
        mPop = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        tvPhoto = (TextView) view.findViewById(R.id.tv_add_picture_photograph);
        tvAlbum = (TextView) view.findViewById(R.id.tv_add_picture_album);
        tvCancel = (TextView) view.findViewById(R.id.tv_add_picture_cancel);
        //
        ColorDrawable colorDrawable = new ColorDrawable(-0000);
        mPop.setBackgroundDrawable(colorDrawable);
        mPop.setOutsideTouchable(true);
        mPop.setFocusable(true);
        mPop.setAnimationStyle(R.style.addPicturePopupWindowAnimation);
        //
        tvPhoto.setOnClickListener(this);
        tvAlbum.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    /**
     * 按键监听
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_add_picture_photograph:// 照相
            if (pictureClick != null) {
                pictureClick.onPictureClick(v, 0);
            }
            dismissWindow();
            break;
        case R.id.tv_add_picture_album:// 相册
            if (pictureClick != null) {
                pictureClick.onPictureClick(v, 1);
            }
            dismissWindow();
            break;
        case R.id.tv_add_picture_cancel:// 取消
            dismissWindow();
            break;
        }
    }

    /**
     * 确定按键监听
     * 
     * @param l
     */
    public void setOnPictureClickListener(OnPictureClickListener d) {
        this.pictureClick = d;
    }

    // 接口引用
    OnPictureClickListener pictureClick;

    /**
     * 确定按键回调接口
     * 
     * @author ZhangXuanChen
     * @created 2013-7-21
     */
    public interface OnPictureClickListener {
        void onPictureClick(View v, int what);
    }

    /**
     * 显示窗口
     */
    public void showWindow(View v) {
        if (mPop != null) {
            if (mPop.isShowing()) {
                mPop.dismiss();
            } else {
                mPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
            }
        }
    }

    /**
     * 关闭窗口
     */
    public void dismissWindow() {
        if (mPop != null) {
            if (mPop.isShowing()) {
                mPop.dismiss();
            }
        }
    }
}

代码中通过工具类对外提供的接口调用,通过继承接口实现onPictureClick方法,代码如下

public void onPictureClick(View v, int what) {
    switch (what) {
        case 0:// 拍照
            Intent picture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            picture.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
            startActivityForResult(picture, ClientConstant.REQUEST_CODE_TAKE_PHOTO);
            break;
        case 1:// 相册
            Intent album = new Intent(Intent.ACTION_PICK, null);
            album.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(album, ClientConstant.REQUEST_CODE_PHOTO_ALBUM);
            break;
        }
    }

对popwindow的声明、设置监听也是必不可少的部分

AddPictureWindow mAddPictureWindow;// 添加拍照window

mAddPictureWindow = new AddPictureWindow(this);初始化popwindow
/**
 * popwindow按键监听
 */
mAddPictureWindow.setOnPictureClickListener(this);

通过点击事件触发弹出popwindow

    /**
     * 按键监听
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.iv_dialogue_addPicture:// 选择图片
            if (mAddPictureWindow != null) {
                mAddPictureWindow.showWindow(v);
            }
            break;
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值