自定义dialog

本文介绍了一个名为ZpopView的自定义对话框类的实现,该类继承自Android的Dialog,用于创建一个包含拍照、相册选择和取消按钮的弹出窗口。文章详细展示了如何设置布局、初始化按钮及响应点击事件,以及如何通过接口回调处理不同按钮的点击行为。
摘要由CSDN通过智能技术生成

public class ZpopView extends Dialog {
    public Context context;

    public ZpopView(Context context) {
        super(context);
        this.context = context;
    }

    public ZpopView(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    protected ZpopView(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context, R.layout.itempop, null);
        setContentView(view);
        Button paizhao = view.findViewById(R.id.paizhao);
        Button xiangce = view.findViewById(R.id.xiangce);
        Button quxiao = view.findViewById(R.id.quxiao);

        paizhao.setOnClickListener(new onClickPop());
        xiangce.setOnClickListener(new onClickPop());
        quxiao.setOnClickListener(new onClickPop());

    }

    public class onClickPop implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.paizhao:
                    httpClick.onhttpclick1();
                    break;
                case R.id.xiangce:
                    httpClick.onhttpclick2();
                    break;
                case R.id.quxiao:
                    httpClick.onhttpclick3();
                    break;
            }
        }
    }

    private HttpClick httpClick;

    public void setHttpClick(HttpClick httpClick) {
        this.httpClick = httpClick;
    }

    public interface HttpClick {
        void onhttpclick1();

        void onhttpclick2();

        void onhttpclick3();
    }
}
public class TwoActivity extends AppCompatActivity {

    private ImageView name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        name = findViewById(R.id.name);
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZpopView zpopView = new ZpopView(TwoActivity.this);
                zpopView.show();
                zpopView.setHttpClick(new ZpopView.HttpClick() {
                    @Override
                    public void onhttpclick1() {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.addCategory("android.intent.category.DEFAULT");
                        startActivityForResult(intent, 0);
                    }

                    @Override
                    public void onhttpclick2() {

                    }

                    @Override
                    public void onhttpclick3() {

                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                Bitmap data1 = data.getParcelableExtra("data");
                name.setImageBitmap(data1);
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值