Android 自定义对话框

为了提高用户体验,达到理想的效果,一般不直接使用系统提供的对话框,而使用自定义的对话框。

步骤:

1、创建对话框的布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <!--标题部分-->
 8     <TextView
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="提示信息"
12         android:gravity="center"
13         android:layout_margin="30dp"
14         android:textSize="20sp"/>
15 
16     <!--提示消息部分-->
17     <TextView
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:id="@+id/tipInfo"
21         android:text="消息内容"
22         android:gravity="center"/>
23 
24     <!--按钮部分-->
25     <LinearLayout
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:orientation="horizontal"
29         android:layout_margin="30dp">
30         <Button
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:id="@+id/okBtn"
34             android:text="确定"/>
35         <Button
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:layout_marginLeft="20dp"
39             android:id="@+id/cancelBtn"
40             android:text="取消"/>
41     </LinearLayout>
42 
43 </LinearLayout>

2、编写自定义对话框对应的类,需要继承Dialog类。这个类我们一般写在一个单独的.java文件中。

 1 package com.example.myapplication;
 2 
 3 import android.app.Dialog;
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.Window;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 //定义一个对话框的类,必须继承Dialog,注意是android.app.Dialog类,不是java.awt.Dialog
12 public class MyDialog extends Dialog {
13     private String msg;  //提示信息
14     private TextView msgView;   //提示消息控件
15     private Button okBtn;   //确定按钮
16     private Button cancelBtn;   //取消按钮
17 
18     //构造函数
19     public MyDialog(Context context,String msg){
20         super(context);  //必须要调用基类的这个构造函数,必须要Context的形参
21         this.msg=msg;
22     }
23 
24     //必须重写onCreate()方法
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);  //自带的,不能缺省
28         requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉默认的标题
29         setContentView(R.layout.my_dialog);   //设置对应的布局文件
30 
31         //成员变量会被这些同名的局部变量屏蔽,当然也可以用其他变量名,无影响
32         TextView msgView=findViewById(R.id.tipInfo);
33         Button okBtn=findViewById(R.id.okBtn);
34         Button cancelBtn=findViewById(R.id.cancelBtn);
35 
36         //设置消息内容
37         msgView.setText(msg);
38 
39         //为2个按钮设置事件监听
40         okBtn.setOnClickListener(new View.OnClickListener() {
41             @Override
42             public void onClick(View v) {
43                 //.........
44             }
45         });
46         cancelBtn.setOnClickListener(new View.OnClickListener() {
47             @Override
48             public void onClick(View v) {
49                 //......
50             }
51         });
52 
53     }
54 
55     //操作本类成员的方法,比如setXxx()、getXxx()。当然不写也ok,如果不写,也可以省略大部分的成员变量
56     //......
57 
58 }

3、使用自定义的对话框

1  //在Activity(.java文件)中使用自定义的对话框
2         MyDialog myDialog=new MyDialog(this,"确定退出吗?");
3         myDialog.show();   //这是继承自基类的方法,可直接用
原文地址:https://www.cnblogs.com/chy18883701161/p/10872765.html