自定義Dialog實(shí)現(xiàn)“退出應(yīng)用程序”效果

退出app.jpeg

引言

??通常在做退出應(yīng)用程序的時(shí)侯戳气,我們需要用到自定義Dialog來實(shí)現(xiàn)退出應(yīng)用程序的彈出框。而且還需要進(jìn)行點(diǎn)擊事件的業(yè)務(wù)邏輯處理诚纸,今天就帶你手把手來實(shí)現(xiàn)自定義Dialog的功能丈钙。


效果預(yù)覽

自定義Dialog.gif

傳送門

[兄弟篇]自定義Dialog添加“顯示背景變暗+消失恢復(fù)亮度”效果


用法

第一步:布局文件(主)

<?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"
    android:orientation="vertical"
    android:background="@color/gray"
    tools:context=".show.Case2">

    <TextView
        android:id="@+id/end_app"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="退出登錄!"
        android:textColor="@color/white"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

第二步:創(chuàng)建SelfDialog類

/**
 * @data on 2020/10/9 10:49 AM
 * @auther armstrong
 * @describe 自定義Dialog
 */
public class SelfDialog extends Dialog {
    private Button yes;//確定按鈕
    private Button no;//取消按鈕
    private TextView titleTv;//消息標(biāo)題文本
    private TextView messageTv;//消息提示文本
    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 SelfDialog(@NonNull Context context) {
        super(context,R.style.MyDialog); //構(gòu)造方法中設(shè)置Dialog的樣式
    }


    /**
     * 設(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 onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydialog_layout);
        //按空白處不能取消動(dòng)畫
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面數(shù)據(jù)
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的確定和取消監(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.onYesClick();
                }
            }
        });
        //設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的顯示數(shù)據(jù)
     */
    private void initData() {
        //如果用戶自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            messageTv.setText(messageStr);
        }
        //如果設(shè)置按鈕的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (noStr != null) {
            no.setText(noStr);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        titleTv = (TextView) findViewById(R.id.title);
        messageTv = (TextView) findViewById(R.id.message);
    }

    /**
     * 從外界Activity為Dialog設(shè)置標(biāo)題
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 從外界Activity為Dialog設(shè)置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }

    /**
     * 設(shè)置確定按鈕和取消被點(diǎn)擊的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}

第三步:設(shè)置Dialog的樣式

<!--自定義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布局文件(子)

slefDialog布局文件layout/mydialog_layout.xml

Tips: 一個(gè)標(biāo)題+提示語+取消按鈕+確定按鈕

<?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:layout_margin="15dp"
            android: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: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_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="No"
                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_marginRight="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="Yes"
                android:textColor="#38ADFF"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

形狀布局文件drawable/free_dialog_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--填充色-->
    <solid android:color="#ffffff" />
    <!--邊線顏色-->
    <stroke
        android:width="0.8dp"
        android:color="#ffffff" />
    <!-- 圓角 -->
    <corners android:radius="6dp" />
</shape>

第五步:在Activity中書寫業(yè)務(wù)邏輯代碼

/**
 * @data on 2020/8/25 11:30 AM
 * @auther      ArmStrong
 * @describe  退出應(yīng)用程序
 */
public class Case2 extends AppCompatActivity {
    private SelfDialog selfDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case2);
        findViewById(R.id.end_app).setOnClickListener(View->{alert();});  //我用的Lambda表達(dá)式剃幌,你自己按基礎(chǔ)寫法寫
    }
    private void alert(){
        selfDialog = new SelfDialog(this);
        selfDialog.setTitle("退出應(yīng)用程序");
        selfDialog.setMessage("確定退出應(yīng)用?");
        selfDialog.setYesOnclickListener("確定", new SelfDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                ToastUtils.show("確定"); //消息提示(我用的Toast第三方庫聋涨,你自己用基礎(chǔ)的Toast寫)
                selfDialog.dismiss();
            }
        });
        selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                ToastUtils.show("取消"); //消息提示
                selfDialog.dismiss();
            }
        });
        selfDialog.show();
    }
}

大功告成!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锥忿,一起剝皮案震驚了整個(gè)濱河市牛郑,隨后出現(xiàn)的幾起案子怠肋,更是在濱河造成了極大的恐慌敬鬓,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件笙各,死亡現(xiàn)場(chǎng)離奇詭異钉答,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)杈抢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門数尿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人惶楼,你說我怎么就攤上這事右蹦≌锔耍” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵何陆,是天一觀的道長晨汹。 經(jīng)常有香客問我,道長贷盲,這世上最難降的妖魔是什么淘这? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮巩剖,結(jié)果婚禮上铝穷,老公的妹妹穿的比我還像新娘。我一直安慰自己佳魔,他們只是感情好曙聂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鞠鲜,像睡著了一般筹陵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上镊尺,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天朦佩,我揣著相機(jī)與錄音,去河邊找鬼庐氮。 笑死语稠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的弄砍。 我是一名探鬼主播仙畦,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼音婶!你這毒婦竟也來了慨畸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤衣式,失蹤者是張志新(化名)和其女友劉穎寸士,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碴卧,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡弱卡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了住册。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婶博。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖荧飞,靈堂內(nèi)的尸體忽然破棺而出凡人,到底是詐尸還是另有隱情名党,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布挠轴,位于F島的核電站兑巾,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏忠荞。R本人自食惡果不足惜蒋歌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望委煤。 院中可真熱鬧堂油,春花似錦、人聲如沸碧绞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽讥邻。三九已至迫靖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間兴使,已是汗流浹背系宜。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留发魄,地道東北人盹牧。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像励幼,于是被迫代替她去往敵國和親汰寓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354