自定义对话框 Dialog

//自定义AddressDialog 继承Dialog 
public class AddressDialog extends Dialog {
//声明对话框里面的控件 
    private Context context;
    private EditText etReceiveName;//接收人姓名
    private EditText etAddress;
    private EditText etCode;//邮编
    private EditText etPhone;
    private EditText etTel;
    private Button btnSubmit; //提交按钮
    private Callback callback;

    public AddressDialog(Context context,Callback callback ) {
        super(context);
        this.context = context;
        this.callback = callback;
    }

    public AddressDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        // 设置对话框背景颜色
        window.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //加载对话框的布局文件
        View dialogView = View.inflate(context, R.layout.dialog_add_address, null);
        window.setContentView(dialogView);
        //初始化对话框里面的控件
        setViews(dialogView);
        //给提交地址按钮设置监听器
        setListener();
    }

    private void setListener() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Address address = new Address();
                address.setReceiveName(etReceiveName.getText().toString());
                address.setFull_address(etAddress.getText().toString());
                address.setMobile(etPhone.getText().toString());
                address.setPostalCode(etCode.getText().toString());
                address.setPhone(etTel.getText().toString());
                //回传到我的地址管理界面 把在对话框里面填写的这条地址添加到地址列表项中
                callback.onSubmit(address);
            }
        });

    }

    private void setViews(View dialogView) {
        etReceiveName = (EditText) dialogView.findViewById(R.id.etReceiveName);
        etAddress = (EditText) dialogView.findViewById(R.id.etReceiveAddress);
        etCode = (EditText) dialogView.findViewById(R.id.etCode);
        etPhone = (EditText) dialogView.findViewById(R.id.etPhone);
        etTel = (EditText) dialogView.findViewById(R.id.etTel);
        btnSubmit = (Button) dialogView.findViewById(R.id.btnAddressSubmit);
    }

    public interface Callback{
        void onSubmit(Address address);
    }
}

你可能感兴趣的:(自定义,对话框)