这是自己写的第一篇博客,倒不是刻意为之,权当是做笔记,以后忘记了还可以回头看看;
布局如下:
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("请先填写邮箱号"); } }