Android自定义dialog

public class MyAuthenticationPhoneDialog extends Dialog {

private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private TextView mTvPhoneYes;//确认按钮
private TextView mTvPhoneNo;//取消按钮

public MyAuthenticationPhoneDialog(@NonNull Context context, @StyleRes int themeResId) {
    super(context, themeResId);
}

/**
 * 设置确定按钮的显示内容和监听
 */
public void setYesOnclickListener(onYesOnclickListener yesOnclickListener) {
    this.yesOnclickListener = yesOnclickListener;
}

/**
 * 设置取消按钮的显示内容和监听
 */
public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {
    this.noOnclickListener = onNoOnclickListener;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_phone_card_layout);
    //点击空白处消失dialog
    setCanceledOnTouchOutside(true);

    //初始化界面控件
    initView();

    //初始化界面控件的事件
    initEvent();
}

/**
 * 初始化界面控件
 */
private void initView() {
    mTvPhoneYes = (TextView) findViewById(R.id.tv_phone_yes);
    mTvPhoneNo = (TextView) findViewById(R.id.tv_phone_no);
}


/**
 * 初始化界面的确定和取消监听
 */
private void initEvent() {
    //设置确定按钮被点击后,向外界提供监听
    mTvPhoneYes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (yesOnclickListener != null) {
                yesOnclickListener.onYesOnclick();
            }
        }
    });
    //设置取消按钮被点击后,向外界提供监听
    mTvPhoneNo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (noOnclickListener != null) {
                noOnclickListener.onNoClick();
            }
        }
    });
}

/*
 * 确认
 * */
public interface onYesOnclickListener {
    public void onYesOnclick();
}

/*
 * 取消
 * */
public interface onNoOnclickListener {
    public void onNoClick();
   }
}

去掉dialog的背景黑边框和透明度

 

使用

		//手机认证
        final MyAuthenticationPhoneDialog myAuthenticationPhoneDialog = new MyAuthenticationPhoneDialog(***.this,R.style.MyDialog);
        //确认按钮
        myAuthenticationPhoneDialog.setYesOnclickListener(new MyAuthenticationPhoneDialog.onYesOnclickListener() {
            @Override
            public void onYesOnclick() {
                myAuthenticationPhoneDialog.dismiss();
            }
        });
        //取消按钮
        myAuthenticationPhoneDialog.setNoOnclickListener(new MyAuthenticationPhoneDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                myAuthenticationPhoneDialog.dismiss();
            }
        });
        myAuthenticationPhoneDialog.show();//显示

你可能感兴趣的:(Android自定义dialog)