简单先创建一个类
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_dialogbox_otp);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView text = (TextView) dialog.findViewById(R.id.txt_file_path);
text.setText(msg);
Button dialogBtn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
dialogBtn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
Button dialogBtn_okay = (Button) dialog.findViewById(R.id.btn_okay);
dialogBtn_okay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
dialog.show();
}
}
然后创建一个 custom_dialogbox_otp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="330dp"
android:layout_height="160dp"
android:background="#00555555"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_layout_otp"
android:orientation="vertical"
android:padding="7dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:gravity="center">
<ImageView
android:id="@+id/a"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#DA5F6A"
android:gravity="center"
android:scaleType="fitCenter"
android:src="@mipmap/infoonetwo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20">
<TextView
android:id="@+id/txt_file_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:singleLine="true"
android:text="TEXTO"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:background="@drawable/round_layout_white_otp"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="60">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Do you wanna Exit..?"
android:textColor="#ff000000"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="30dp"
android:layout_weight="50"
android:gravity="center|right">
<Button
android:id="@+id/btn_cancel"
android:layout_width="80dp"
android:layout_height="25dp"
android:background="@drawable/round_button"
android:gravity="center"
android:text="CANCEL"
android:textSize="13dp"
android:textStyle="bold"
android:textColor="#ffffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_weight="50"
android:gravity="center|left">
<Button
android:id="@+id/btn_okay"
android:layout_width="80dp"
android:layout_height="25dp"
android:background="@drawable/round_button"
android:text="OKAY"
android:textSize="13dp"
android:textStyle="bold"
android:textColor="#ffffffff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后在你的drawable中创建xml文件。
对于 round_layout_white_otp.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- <corners android:radius="10dp" /> -->
<corners
android:bottomLeftRadius="18dp"
android:bottomRightRadius="16dp"
android:topLeftRadius="38dp"
android:topRightRadius="36dp" />
<solid android:color="#C0C0C0" />
</shape>
对于 round_layout_otp.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- <corners android:radius="10dp" /> -->
<corners
android:bottomLeftRadius="18dp"
android:bottomRightRadius="16dp"
android:topLeftRadius="38dp"
android:topRightRadius="38dp" />
<solid android:color="#DA5F6A" />
</shape>
圆形按钮
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- <corners android:radius="10dp" /> -->
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<solid android:color="#06A19E" />
</shape>
然后最后使用下面的代码来可视化你的对话框:)
ViewDialog alert = new ViewDialog();
alert.showDialog(ReceivingOTPRegActivity.this, "OTP has been sent to your Mail ");
你的输出:)
答案更新:对于 kotlin
class ViewDialog {
fun showResetPasswordDialog(activity: Activity?) {
val dialog = Dialog(activity!!)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.resetpass_popup)
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val dialogBtn_remove = dialog.findViewById<TextView>(R.id.txtClose)
dialogBtn_remove.setOnClickListener {
dialog.dismiss()
activity!!.finish()
}
dialog.show()
}
}
还有你的 xml 设计,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingDefaultResource">
<androidx.cardview.widget.CardView
android:layout_width="340dp"
android:layout_height="290dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
app:cardCornerRadius="11dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="61dp"
android:layout_height="61dp"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:src="@drawable/tickmark"
tools:ignore="ContentDescription"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginTop="18dp"
android:layout_marginRight="12dp"
android:fontFamily="@font/opensans_semibold"
android:text="@string/apasswordreset"
android:textColor="@color/gender_txt"
android:textSize="15dp"
tools:ignore="HardcodedText,RtlHardcoded,SpUsage"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="12dp"
android:fontFamily="@font/opensans_semibold"
android:text="@string/toyourmail"
android:textColor="@color/gender_txt"
android:textSize="15dp"
tools:ignore="HardcodedText,RtlHardcoded,SpUsage">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginTop="18dp"
android:layout_marginRight="12dp"
android:fontFamily="@font/opensans_regular"
android:text="@string/followthe"
android:textColor="@color/gender_txt"
android:textSize="14dp"
tools:ignore="HardcodedText,RtlHardcoded,SpUsage"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="12dp"
android:fontFamily="@font/opensans_regular"
android:text="@string/inyouremail"
android:textColor="@color/gender_txt"
android:textSize="14dp"
tools:ignore="HardcodedText,RtlHardcoded,SpUsage"></TextView>
<TextView
android:id="@+id/txtClose"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="38dp"
android:background="@drawable/nextbtn"
android:fontFamily="@font/opensans_semibold"
android:gravity="center"
android:text="Close"
android:textColor="@color/white_o"
android:textSize="16dp"
tools:ignore="HardcodedText,RtlHardcoded,SpUsage"></TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
现在,您可以致电
val alert = ViewDialog()
alert.showResetPasswordDialog(activity)
而你的结果,