自定義 dialog

安卓系統(tǒng)的提供的dialog往往不能滿足實(shí)際項(xiàng)目要求,此時(shí)就需要根據(jù)需求實(shí)現(xiàn)自定view醉途。通常dialog作為一種提示性對(duì)話框來(lái)使用洒缀,不需要從外界獲取數(shù)據(jù)衷佃,然后展示在dialog上,就像退出時(shí)提示框又或者額是登陸時(shí)發(fā)生錯(cuò)誤贮庞,進(jìn)行簡(jiǎn)單的登錄異常信息提示峦筒;而另外一種用途通常需要從外界獲取數(shù)據(jù)作為dialog的view來(lái)進(jìn)行展示,此時(shí)就需要自定義實(shí)現(xiàn)窗慎,點(diǎn)擊某個(gè)數(shù)據(jù)項(xiàng)之后物喷,觸發(fā)相關(guān)操作;


自定義dialog思路解析:
1dialog的界面布局遮斥;
2外界傳入的數(shù)據(jù)峦失;(有些dialog不需要數(shù)據(jù),就像退出提示框一樣伏伐,只有宠进,取消和確定操作,有些dialog是需要數(shù)據(jù)的藐翎,這些數(shù)據(jù)決定dialog的具體的item項(xiàng))
3定義接口材蹬,添加監(jiān)聽,實(shí)現(xiàn)交互功能吝镣;


第一種簡(jiǎn)單提示框的實(shí)現(xiàn)(不需要從外界獲取數(shù)據(jù)):
效果演示:

showdialog.gif

<strong>第一步:定義dialog的布局文件layout_moresetdialog.xml堤器;</strong>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:orientation="vertical"
    android:background="@drawable/free_dialog_bg">
    <TextView
        android:id="@+id/set_more_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="查看更多"
        android:textSize="14dp"
        android:textColor="@color/blue"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        />
    <View
        android:id="@+id/set_more_title_view"
        android:layout_below="@+id/set_more_title"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#e6e6e6" />
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:layout_below="@+id/set_more_title_view">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:id="@+id/hand_up_btn"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="舉手/放下"
                android:background="@null"
                android:drawableLeft="@mipmap/hand_up"
                android:gravity="left|center_vertical"
                android:drawablePadding="28dp"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="#e6e6e6" />
            <Button
                android:id="@+id/net_switch_btn"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="優(yōu)選網(wǎng)絡(luò)"
                android:background="@null"
                android:drawableLeft="@mipmap/icon_more_netswitch"
                android:gravity="left|center_vertical"
                android:drawablePadding="30dp"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="#e6e6e6" />
            <Button
                android:id="@+id/error_report_btn"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="故障報(bào)告"
                android:background="@null"
                android:drawableLeft="@mipmap/icon_more_about"
                android:gravity="left|center_vertical"
                android:drawablePadding="30dp"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="#e6e6e6" />
            <Button
                android:id="@+id/play_switch_btn"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="切換"
                android:background="@null"
                android:drawableLeft="@mipmap/icon_more_switch"
                android:gravity="left|center_vertical"
                android:drawablePadding="30dp"/>
        </LinearLayout>

    </ScrollView>
    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/dialog_close_btn"
            android:layout_below="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="30dp"
            android:text="關(guān)閉"
            android:textColor="@color/black"
            android:background="@drawable/close_btn_bg"

            />

    </LinearLayout>
</LinearLayout>

<strong>第二步:為每個(gè)item項(xiàng)定義監(jiān)聽,供外界調(diào)用末贾,提供相應(yīng)的事件響應(yīng)</strong>

/**
     * 提供設(shè)置監(jiān)聽的方法
     */
    public void setOnBtnClickListener(OnBtnClickListener listener){
        this.listener=listener;
    }

    /**
     * item項(xiàng)事件監(jiān)聽接口
     */
    public interface OnBtnClickListener{
        public void onHandUp();
        public void onNetSwitch();
        public void onErrorReport();
        public void onPlaySwitch();
    }

    private void onClose(){
        if (this.isShowing()){
            dismiss();
        }
    }

完整代碼如下:

/**
 * @author: Created by lzl on 2017/6/13.
 * @function:
 * @description:
 */

public class MoreSetDialog extends Dialog {
    private Button mHandup;//舉手放下
    private Button mNetSwitch;//優(yōu)選網(wǎng)絡(luò)
    private Button mErrorReport;//錯(cuò)誤報(bào)告
    private Button mPlaySwitch;//視頻與文檔切換
    private Button mCloseBtn;//關(guān)閉
    private OnBtnClickListener listener;//內(nèi)部監(jiān)聽器


    public MoreSetDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_moresetdialog);
        //點(diǎn)擊外部區(qū)域不可取消
        setCanceledOnTouchOutside(false);
        initView();
        initEvent();
    }

    private void initEvent() {
        //舉手/放下事件
        mHandup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onHandUp();
                }
            }
        });
        //網(wǎng)絡(luò)優(yōu)選事件
        mNetSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onNetSwitch();
                }
            }
        });
        //錯(cuò)誤報(bào)告事件
        mErrorReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onErrorReport();
                }
            }
        });
        //視頻模塊與文檔模塊切換事件
        mPlaySwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onPlaySwitch();
                }
            }
        });
        //關(guān)閉事件
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClose();
            }
        });
    }

    /**
     * 初始化布局文件
     */
    private void initView() {
        mHandup= (Button) findViewById(R.id.hand_up_btn);
        mNetSwitch= (Button) findViewById(R.id.net_switch_btn);
        mErrorReport= (Button) findViewById(R.id.error_report_btn);
        mPlaySwitch= (Button) findViewById(R.id.play_switch_btn);
        mCloseBtn= (Button) findViewById(R.id.dialog_close_btn);
    }

    /**
     * 提供設(shè)置監(jiān)聽的方法
     */
    public void setOnBtnClickListener(OnBtnClickListener listener){
        this.listener=listener;
    }

    /**
     * item項(xiàng)事件監(jiān)聽接口
     */
    public interface OnBtnClickListener{
        public void onHandUp();
        public void onNetSwitch();
        public void onErrorReport();
        public void onPlaySwitch();
    }

    private void onClose(){
        if (this.isShowing()){
            dismiss();
        }
    }

}

調(diào)用如下:

private void shoDialog() {
        final MoreSetDialog dialog=new MoreSetDialog(this);
        dialog.setOnBtnClickListener(new MoreSetDialog.OnBtnClickListener() {
            @Override
            public void onHandUp() {
                Toast.makeText(getApplicationContext(),"舉手",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNetSwitch() {
                dialog.dismiss();
                Toast.makeText(getApplicationContext(),"優(yōu)選網(wǎng)絡(luò)",Toast.LENGTH_SHORT).show();
                GsNetSwitchDialog gsNetSwitchDialog=new GsNetSwitchDialog(MoreDialogActivity.this);
                gsNetSwitchDialog.show();
            }

            @Override
            public void onErrorReport() {
                Toast.makeText(getApplicationContext(),"故障報(bào)告",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPlaySwitch() {
                Toast.makeText(getApplicationContext(),"切換",Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();

    }

引用的自定義drawable文件

<?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="10dp" />

</shape>

dialog的style屬性:

<!--自定義dialog背景全透明無(wú)邊框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>

一些注意的問題:

//點(diǎn)擊外部區(qū)域不可取消,false時(shí)表示點(diǎn)擊外部區(qū)域不隱藏該dialog闸溃,true時(shí)點(diǎn)擊外部區(qū)域取消該dialog
setCanceledOnTouchOutside(false);
2內(nèi)部的監(jiān)聽接口最好定義接口,具體響應(yīng)操作由外部調(diào)用者實(shí)現(xiàn)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市辉川,隨后出現(xiàn)的幾起案子表蝙,更是在濱河造成了極大的恐慌,老刑警劉巖乓旗,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件府蛇,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡屿愚,警方通過(guò)查閱死者的電腦和手機(jī)汇跨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)妆距,“玉大人穷遂,你說(shuō)我怎么就攤上這事∮榫荩” “怎么了蚪黑?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)吸耿。 經(jīng)常有香客問我祠锣,道長(zhǎng),這世上最難降的妖魔是什么咽安? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任伴网,我火速辦了婚禮,結(jié)果婚禮上妆棒,老公的妹妹穿的比我還像新娘澡腾。我一直安慰自己,他們只是感情好糕珊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布动分。 她就那樣靜靜地躺著,像睡著了一般红选。 火紅的嫁衣襯著肌膚如雪澜公。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天喇肋,我揣著相機(jī)與錄音坟乾,去河邊找鬼。 笑死蝶防,一個(gè)胖子當(dāng)著我的面吹牛甚侣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播间学,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼殷费,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼印荔!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起详羡,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤仍律,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后实柠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體染苛,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年主到,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片躯概。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡登钥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出娶靡,到底是詐尸還是另有隱情牧牢,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布姿锭,位于F島的核電站塔鳍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏呻此。R本人自食惡果不足惜轮纫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望焚鲜。 院中可真熱鬧掌唾,春花似錦、人聲如沸忿磅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)葱她。三九已至撩扒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吨些,已是汗流浹背搓谆。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留锤灿,地道東北人挽拔。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像但校,于是被迫代替她去往敵國(guó)和親螃诅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容