AndroidStudio中实现AlertDialog对话框与自定义对话框制作
————安德风QQ1652102745
一、最终效果演示:
二、布局设计
①activity_main.xml源代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity" > 9 10 <Button 11 android:id="@+id/btn_1" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:onClick="MyClick" 15 android:text="对话框" /> 16 17 <Button 18 android:id="@+id/btn_2" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="自定义对话框" 22 android:onClick="MyClick" 23 /> 24 </LinearLayout>
②自定义对话框布局dialog_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:layout_gravity="center_horizontal" 9 android:background="@drawable/dialog_bg"> 10 11 <TextView 12 android:id="@+id/textView" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginTop="356dp" 16 android:text="您确定要退出吗?" 17 android:textColor="#EF0A0A" 18 android:textSize="40sp" 19 android:textStyle="bold" 20 app:layout_constraintEnd_toEndOf="parent" 21 app:layout_constraintHorizontal_bias="0.62" 22 app:layout_constraintStart_toStartOf="parent" 23 app:layout_constraintTop_toTopOf="parent" /> 24 25 <LinearLayout 26 android:layout_width="409dp" 27 android:layout_height="184dp" 28 android:orientation="horizontal" 29 app:layout_constraintBottom_toBottomOf="parent" 30 app:layout_constraintEnd_toEndOf="parent" 31 app:layout_constraintHorizontal_bias="0.487" 32 app:layout_constraintStart_toStartOf="parent" 33 app:layout_constraintTop_toBottomOf="@+id/textView" 34 android:gravity="center_vertical" 35 app:layout_constraintVertical_bias="0.251"> 36 37 <Button 38 android:id="@+id/yesbutton" 39 android:layout_width="80dp" 40 android:layout_height="50dp" 41 android:layout_weight="1" 42 android:layout_marginLeft="40dp" 43 android:background="@drawable/yes_btn" /> 44 45 <Button 46 android:id="@+id/nobutton" 47 android:layout_width="80dp" 48 android:layout_height="50dp" 49 android:layout_marginRight="40dp" 50 android:layout_marginLeft="20dp" 51 android:layout_weight="1" 52 android:background="@drawable/no_btn" /> 53 </LinearLayout> 54 </androidx.constraintlayout.widget.ConstraintLayout>
三、对话框功能实现
1、普通对话框和自定义对话框功能(导入样式功能)MainActivity.java
1 package com.example.adf520; 2 3 import androidx.appcompat.app.AlertDialog; 4 import androidx.appcompat.app.AppCompatActivity; 5 6 import android.app.Dialog; 7 import android.content.DialogInterface; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Button; 11 12 public class MainActivity extends AppCompatActivity { 13 Button btn_1,btn_2;//声明对话框按钮、自定义对话框 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 btn_1=findViewById(R.id.btn_1);//寻找对话框按钮ID 20 btn_2=findViewById(R.id.btn_2);//寻找自定义对话框按钮ID 21 22 23 } 24 //实现按钮交互功能 25 public void MyClick(View view) { 26 switch (view.getId()){ //通过多分支选择判断:当选中对话框时,执行对话框按钮 27 case R.id.btn_1: 28 AlertDialog.Builder builder=new AlertDialog.Builder(this);//AlertDialog创建一个Builder实例化建立器 29 //通过builder实例化建立器来调用对话框属性: 30 builder.setTitle("提示"); //设置对话框标题 31 builder.setMessage("您是否退出?");//设置对话框信息提示 32 builder.setNegativeButton("NO",null);//对话框信息提示,选择取消 33 builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 34 //对话框信息提示,选项按钮,选择"YES"将执行DialogInterface监听器里面方法,执行退出 35 @Override 36 public void onClick(DialogInterface dialog, int which) { 37 finish(); 38 } 39 }); 40 41 builder.show();//对话框展示 42 break; 43 //通过多分支选择判断:当选中对话框时,执行自定义对话框按钮 44 case R.id.btn_2: 45 mydialog mydialog=new mydialog(this,R.style.mydialog); 46 //实例化mydialog设置参数( 参数1:环境上下文 (这里设置this), 参数2:导入样式R.style/样式名) 47 mydialog.show();//展示效果 48 break; 49 } 50 } 51 52 // public void show(){ 53 // AlertDialog dialog=new AlertDialog.Builder(this).create(); 54 // dialog.setTitle("提示"); 55 // dialog.setMessage("您确定要退出吗?"); 56 // dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { 57 // @Override 58 // public void onClick(DialogInterface dialog, int which) { 59 // 60 // } 61 // }); 62 // dialog.show(); 63 // } 64 65 66 67 68 }
2、mydialog.java自定义对话框功能实现(两个判断按钮交互功能)
1 package com.example.adf520; 2 3 import android.app.Dialog; 4 import android.content.Context; 5 import android.view.View; 6 7 import androidx.annotation.NonNull; 8 9 public class mydialog extends Dialog { 10 11 public mydialog(@NonNull Context context, int themeResId) { 12 super(context, themeResId); 13 setContentView(R.layout.dialog_main);//输出自定义对话框布局 14 15 /**总结自定义对话框步骤: 16 * ①设计自定义对话框布局样式--》dialog_layout.xml 17 * ②设计style文件(关闭自定义对话框的标题、去除背景(把背景设置成透明色)) 18 *③ 将第一步的布局应用到当前自定义对话框(mydialog.java中设置,另外也要实现YES按钮和NO按钮的响应功能) 19 * ④在MainActivity.java实例化对话框(参数1:环境上下文(默认设置:this),参数2:导入样式R.style/样式名 ;并且show()方法展示出对话框效果) 20 */ 21 22 23 24 25 //YES按钮安装监听器,实现响应功能 26 findViewById(R.id.yesbutton).setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 System.exit(0); //自定义对话框点击YES按钮时,执行退出 30 } 31 }); 32 //NO按钮安装监听器,实现响应功能 33 findViewById(R.id.nobutton).setOnClickListener(new View.OnClickListener() { 34 @Override 35 public void onClick(View v) { 36 dismiss(); //自定义对话框点击NO按钮时,取消 37 } 38 }); 39 } 40 }
四、自定义对话框样式设置,Res/style.xml
1 <resources> 2 3 <!-- Base application theme. --> 4 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 5 <!-- Customize your theme here. --> 6 <item name="colorPrimary">@color/colorPrimary</item> 7 <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 8 <item name="colorAccent">@color/colorAccent</item> 9 </style> 10 <!-- 设置自定义对话框样式:①导入对话框样式模板 --> 11 <style name="mydialog" parent="android:style/Theme.Dialog"> 12 <item name="android:windowNoTitle">true</item> 13 <!-- 设置关闭自定义对话框标题 --> 14 <item name="android:windowBackground">@android:color/transparent</item> 15 <!-- 设置自定义对话框背景颜色为透明 --> 16 </style> 17 18 </resources>