关闭

接口回调实现popwindow展示

标签: popwindow接口回调
225人阅读 评论(0) 收藏 举报
分类:

常用的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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:665次
    • 积分:94
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条