一:AlterDialog對話框
二:普通對話框
運行效果:
代碼詳情
備註:onBackPressed()事件是用戶點擊回退鍵退出軟件時觸發,事件由系統自己監聽。
三:單選對話框
運行效果:
代碼詳情
四:多選對話框
運行效果:
代碼詳情
五:自定義對話框
運行效果:
1:自定義對話框dialog.xml佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#11ffffff">
<LinearLayout
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/radius_bomb_box"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="警告!!!"
android:textColor="#38ADFF"
android:textSize="16sp"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center"
android:text="這是自定義對話框"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="取消"
android:textColor="#7D7D7D"
android:textSize="16sp"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4"/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="確定"
android:textColor="#38ADFF"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
2:對話框圓角顯示(在drawable下創建radius_bomb_box.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<solid android:color="#ffffff" />
<stroke
android:width="0.8dp"
android:color="#ffffff" />
<!-- 圓角 -->
<corners android:radius="6dp" />
</shape>
3:Styles樣式設置
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--自定義Dialog背景全透明無邊框theme-->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景顏色和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標題-->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
4:創建MyDialog對話框類(繼承Dialog類)
package cn.cg.promptbox;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
private Button yes;//確定按鈕
private Button no;//取消按鈕
private TextView titleTV;//消息標題文本
private TextView message;//消息提示文本
private String titleStr;//從外界設置的title文本
private String messageStr;//從外界設置的消息文本
//確定文本和取消文本的顯示的內容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按鈕被點擊了的監聽器
private onYesOnclickListener yesOnclickListener;//確定按鈕被點擊了的監聽器
public MyDialog(Context context, int themeResId) {
super(context, themeResId);
}
/**
* 設置取消按鈕的顯示內容和監聽
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 設置確定按鈕的顯示內容和監聽
*
* @param str
* @param yesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//佈局視圖
setContentView(R.layout.dialog);
//空白處不能取消動畫
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面數據
initData();
//初始化界面控件的事件
initEvent();
}
/**
* 初始化界面控件
*/
private void initView() {
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
titleTV = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
}
/**
* 初始化界面控件的顯示數據
*/
private void initData() {
//如果用戶自定了title和message
if (titleStr != null) {
titleTV.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
//如果設置按鈕文字
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面的確定和取消監聽
*/
private void initEvent() {
//設置確定按鈕被點擊後,向外界提供監聽
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick();
}
}
});
//設置取消按鈕被點擊後,向外界提供監聽
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
/**
* 從外界Activity爲Dialog設置標題
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
/**
* 從外界Activity爲Dialog設置message
*
* @param message
*/
public void setMessage(String message) {
messageStr = message;
}
//取消按鈕監聽事件
public interface onNoOnclickListener {
public void onNoClick();
}
//確定按鈕監聽事件
public interface onYesOnclickListener {
public void onYesOnclick();
}
}
5:通過自己創建的Dialog 類(MyDialog)進行顯示
/*
自定義對話框
*/
//自定義對話框
private MyDialog myDialog;
private void DisplayCustomDialog(View view) {
myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);
myDialog.setTitle("警告");
myDialog.setMessage("這是自定義對話框!");
myDialog.setYesOnclickListener("確定", new MyDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
//確定點擊事件
Toast.makeText(MainActivity.this,"你點擊了自定義提示框確定按鈕",Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
});
myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
//取消點擊事件
Toast.makeText(MainActivity.this,"你點擊了自定義提示框取消按鈕",Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
});
myDialog.show();
}
備註:自定義對話框來源於來自這裏
關於@null的解釋
最後整個項目下載地址點擊下載吧!