Android 自定义AlertDialog

这是自己写的第一篇博客,倒不是刻意为之,权当是做笔记,以后忘记了还可以回头看看;

布局如下


 alert  xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/confirm_email"
        android:textColor="@color/T_54"
        android:textSize="18sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/confirm_email_detail"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/tv_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/confirm_email"
        android:textSize="14sp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="20dp"
        android:background="@color/T_75"></View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_cancel_send"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1"
            android:text="@string/cancel"
            android:textSize="18sp"
            android:textColor="@color/IMChatTheme"/>
        <View
            android:layout_width="1px"
            android:layout_height="50dp"
            android:background="@color/T_75"></View>
        <TextView
            android:id="@+id/tv_ok_send"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1"
            android:text="@string/ok"
            android:textSize="18sp"
            android:textColor="@color/IMChatTheme"/>
    </LinearLayout>

</LinearLayout>

activity关键代码:

//确认发送验证码邮箱
private void confirmEmail() {
    View view = View.inflate(this, R.layout.alert_recover_password_by_email, null);
    TextView mGetEmail = view.findViewById(R.id.tv_email);
    TextView mCancle = view.findViewById(R.id.tv_cancel_send);
    TextView mOk = view.findViewById(R.id.tv_ok_send);
    mEmailContent = mEmail.getText().toString().trim();
    if (!TextUtils.isEmpty(mEmailContent)) {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(view);
        //设置dialog宽度
        WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
        attributes.width= (int) (ScreenUtils.getScreenWidth(this)*0.9);
        dialog.getWindow().setAttributes(attributes);

        mGetEmail.setText(mEmailContent);
        mCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        mOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送验证码
            }
        });

    } else {
        mEmail.setError("请先填写邮箱号");
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值