Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
本文著重研究了自定義對(duì)話框,通過一下步驟即可清晰的理解原理,通過更改界面設(shè)置和style類型,可以應(yīng)用在各種各樣適合自己的App中。
首先來看一下效果圖:
首先是activity的界面

點(diǎn)擊了上述圖片的按鈕后,彈出對(duì)話框:

點(diǎn)擊對(duì)話框的確定按鈕:

點(diǎn)擊對(duì)話框的取消按鈕:

下面來說一下具體實(shí)現(xiàn)步驟:
第一步:設(shè)置Dialog的樣式(一般項(xiàng)目都可以直接拿來用):style.xml中
<!--自定義Dialog背景全透明無邊框theme--> <style name="MyDialog" parent="android:style/Theme.Dialog"> <!--背景顏色和透明程度--> <item name="android:windowBackground">@android:color/transparent</item> <!--是否去除標(biāo)題--> <item name="android:windowNoTitle">true</item> <!--是否去除邊框--> <item name="android:windowFrame">@null</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsFloating">true</item> <!--是否模糊--> <item name="android:backgroundDimEnabled">false</item> </style>
第二步:自定義Dialog:
(1)自定義的Dialog的布局: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/free_dialog_bg" 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="您的手機(jī)馬上自爆"/> <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)自定義Dialog布局中的背景:free_dialog_bg.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) 自定義的Dialog的java:MyDialog.class
package com.syah.mydialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* 創(chuàng)建自定義的Dialog,主要學(xué)習(xí)實(shí)現(xiàn)原理
* Created by admin on 2017/8/30.
*/
public class MyDialog extends Dialog {
private Button yes;//確定按鈕
private Button no;//取消按鈕
private TextView titleTV;//消息標(biāo)題文本
private TextView message;//消息提示文本
private String titleStr;//從外界設(shè)置的title文本
private String messageStr;//從外界設(shè)置的消息文本
//確定文本和取消文本的顯示的內(nèi)容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽器
private onYesOnclickListener yesOnclickListener;//確定按鈕被點(diǎn)擊了的監(jiān)聽器
public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
}
/**
* 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param yesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
//空白處不能取消動(dòng)畫
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面數(shù)據(jù)
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);
}
/**
* 初始化界面控件的顯示數(shù)據(jù)
*/
private void initData() {
//如果用戶自定了title和message
if (titleStr != null) {
titleTV.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
//如果設(shè)置按鈕文字
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面的確定和取消監(jiān)聽
*/
private void initEvent() {
//設(shè)置確定按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick();
}
}
});
//設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
/**
* 從外界Activity為Dialog設(shè)置標(biāo)題
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
/**
* 從外界Activity為Dialog設(shè)置message
*
* @param message
*/
public void setMessage(String message) {
messageStr = message;
}
public interface onNoOnclickListener {
public void onNoClick();
}
public interface onYesOnclickListener {
public void onYesOnclick();
}
}
第三步:activity中使用自定義對(duì)話框:
(1)activity的布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:context="com.syah.mydialog.MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定義dialog" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
(2) MainActivity.class
package com.syah.mydialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MyDialog myDialog;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);
myDialog.setTitle("警告!");
myDialog.setMessage("警告:您的手機(jī)3秒鐘內(nèi)自爆");
myDialog.setYesOnclickListener("確定", new MyDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
Toast.makeText(getApplicationContext(),"拜拜,我們來生見",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
});
myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(getApplicationContext(),"明智的選擇",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
});
myDialog.show();
}
});
}
}
原理:
1、通過構(gòu)造方法給dialog設(shè)置一個(gè)主題 R.style.MyDialog , 主要設(shè)置dialog的顯示屬性,一般都是 全透明無邊框 ;
2、然后在dialog的onCreate()方法中,用setContentView( R.layout.SelfDialog) 為dialog設(shè)置XML文件,我們就可以在layout文件中創(chuàng)建自定義的Dialog風(fēng)格。這里我就自定義了xml文件格式,實(shí)現(xiàn)了自定義的外觀風(fēng)格,不受系統(tǒng)的主題影響。
3、然后通過設(shè)置要為外界設(shè)置一些public 公開的方法,來向自定義的dialog傳遞值。這里的title 和 message,都是可以通過外界傳值進(jìn)來,進(jìn)行設(shè)置的。如下面的public 方法就是供外界activity來設(shè)置title和message的:
/**
* 從外界Activity為Dialog設(shè)置標(biāo)題
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
/**
* 從外界Activity為Dialog設(shè)置message
*
* @param message
*/
public void setMessage(String message) {
messageStr = message;
}
在activity通過實(shí)例化Dialog后就可以設(shè)置titile和message了。
myDialog=new MyDialog(MainActivity.this);
myDialog.setTitle("警告!");
myDialog.setMessage("警告:您的手機(jī)3秒鐘內(nèi)自爆");
另外在MyDialog.class中通過下面構(gòu)造器可以更靈活的選擇Dialog的類型
public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
}activity中使用自定義的dialog:
myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);
4、最后,自定義的dialog中包含了一些按鈕的時(shí)候,這個(gè)時(shí)候要想讓按鈕有點(diǎn)擊事件,并且把這個(gè)點(diǎn)擊事件能夠傳遞給activity,讓acitvity做一些事情,這里就需要設(shè)置監(jiān)聽接口,讓button的點(diǎn)擊事件能夠讓外界activity知道。如下面的代碼。
/**
* 確定按鈕接口
*/
public interface onNoOnclickListener {
public void onNoClick();
}
/**
* 取消按鈕接口
*/
public interface onYesOnclickListener {
public void onYesOnclick();
}
private onNoOnclickListener noOnclickListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽器
private onYesOnclickListener yesOnclickListener;//確定按鈕被點(diǎn)擊了的監(jiān)聽器
/**
* 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param yesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
//設(shè)置確定按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick();
}
}
});
//設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});activity就可以設(shè)置監(jiān)聽接口來實(shí)時(shí)獲取button的點(diǎn)擊事件如下:
myDialog.setYesOnclickListener("確定", new MyDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
Toast.makeText(getApplicationContext(),"拜拜,我們來生見",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
});
myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(getApplicationContext(),"明智的選擇",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
});以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
- Android對(duì)話框使用方法詳解
相關(guān)文章
Android常用三方庫混淆規(guī)則整理(小結(jié))
這篇文章主要介紹了Android常用三方庫混淆規(guī)則整理(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Android獲取移動(dòng)網(wǎng)絡(luò)信號(hào)強(qiáng)度的方法
這篇文章主要介紹了Android獲取移動(dòng)網(wǎng)絡(luò)信號(hào)強(qiáng)度的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android 圖文詳解Binder進(jìn)程通信底層原理
Android系統(tǒng)中,多進(jìn)程間的通信都是依賴于底層Binder IPC機(jī)制,Binder機(jī)制是一種RPC方案。例如:當(dāng)進(jìn)程A中的Activity與進(jìn)程B中的Service通信時(shí),就使用了binder機(jī)制2021-10-10
Android開發(fā)環(huán)境搭建圖文教程 親測(cè)有效!
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測(cè)試有效的搭建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程實(shí)現(xiàn)的重力感應(yīng)示例代碼
這篇文章主要介紹了Android編程實(shí)現(xiàn)的重力感應(yīng)效果,以完整示例代碼形式分析了重力感應(yīng)的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android開發(fā)之關(guān)閉和打開Speaker(揚(yáng)聲器)的方法
這篇文章主要介紹了Android開發(fā)之關(guān)閉和打開Speaker(揚(yáng)聲器)的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android揚(yáng)聲器的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-03-03
android實(shí)現(xiàn)查詢公交車還有幾站的功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)查詢公交車還有幾站的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android實(shí)現(xiàn)View滑動(dòng)的6種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

