Android随心控制对话框的显示与关闭的几种情况

2016年04月28日 Android 7条评论 阅读7134次

对话框在Android开发中是很常见的,如果驾驭好对话框,想让它关闭就关闭,想让它显示就显示。这里面多多少少的有点坑。

本文把对话框显示与关闭的几种情况罗列一下,以后开发中遇到问题可以做个参考。


  • 点击【确定】或【取消】按钮,执行一些操作处理,对话框自动关闭

        这是默认情况,不解释也不贴代码了。


  • 点击【确定】或【取消】按钮,执行一些操作处理,对话框不关闭
        这种情况下。比较通用的就是利用java的反射机制强制控制mShowing字段的值


@Override
public void onClick(DialogInterface dialog, int which) {
    try {
        // 取得mShowing字段
        field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
        // 使mShowing字段可访问
        field.setAccessible(true);
        // 改变mShowing字段的属性值(false:不自动关闭)
        field.set(dialog, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


  • 点击【确定】或【取消】按钮,执行一些操作处理,对话框不关闭后,在某种情况下,又想再关闭对话框
根据前面说的反射原理,很容易想到把mShowing字段的值改为true。结果发现这样是不可以的。
改为true后只是说明dialog可以自动关闭了,但你并没有执行关闭对话框的语句。
所以要加上一句 dialog.dismiss();

@Override
public void onClick(DialogInterface dialog, int which) {
    try {
        // 取得mShowing字段
        field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
        // 使mShowing字段可访问
        field.setAccessible(true);
        // 改变mShowing字段的属性值(true:自动关闭)
        field.set(dialog, true);
        dialog.dismiss();
    } catch (Exception e) {
        e.printStackTrace();
    }
}



  • 点击【确定】或【取消】按钮,执行一些操作处理,对话框不关闭后,按下手机的back键关闭对话框
大体一看这貌似又和第三种情况是一样的,但当把第三种情况的代码加上执行后,发现在dialog显示时手机上的back键却没有响应了。
要解决dialog展示中back键不响应的问题,主要是为dialog增加按键监听
下面解决dialog中back键不响应的问题的代码

dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode,
            KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            try {
                field.set(dialog, true);
                dialog.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
});






分享本文至:

WRITTEN BY

avatar
本文标签:androiddialog
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

7 条评论

  1. avatar antalyairan

    <a href="http://www.sohbetikiz.com/tennessee-titans-47-nfl-transistor-clean-up-cap-hath">tennessee titans 47 nfl transistor clean up cap</a> <a href="http://www.akkordiki.com/adidas-soccers-zapatos-cielo-azul-naranja-trainersce">adidas soccers zapatos cielo azul naranja</a> <a href="http://www.torrentslot.com/nike-air-max-2019-fluorescent-gr%C3%BCn-schwarz-schuhek">nike air max 2019 fluorescent gr眉n schwarz</a> <a href="http://www.familycarepc.com/nike-air-max-cage-bl%C3%A5-scarpef">nike air max cage bl氓</a> <a href="http://www.alizatrade.com/jordan-2-varsity-rojo-for-venta-kurpesb">jordan 2 varsity rojo for venta</a> <a href="http://www.auroragenri.com/nike-air-zoom-pegasus-32-negro-mujeres-zapatosk">nike air zoom pegasus 32 negro mujeres</a>
    antalyairan http://www.antalyairan.com/

  2. avatar thegillssell

    <a href="http://www.teamdalarna.com/aidas-boost-clima-chill-himmelbl%C3%A5-sort-skoi">aidas boost clima chill himmelbl氓 sort</a> <a href="http://www.syairshamyoun.com/nike-kobe-11-flyknit-rosa-svart-obuvq">nike kobe 11 flyknit rosa svart</a> <a href="http://www.fizalisfresh.com/nike-huarache-utility-red-yellow-shoesh">nike huarache utility red yellow</a> <a href="http://www.fsscas.com/nike-huarache-free-nero-gituttio-scarpei">nike huarache free nero gituttio</a> <a href="http://www.imagimp.com/herren-nike-huarache-utility-lila-orangen-schuhej">herren nike huarache utility lila orangen</a> <a href="http://www.burmamaritime.com/buffalo-bills-color-rush-hat-hath">buffalo bills color rush hat</a>
    thegillssell http://www.thegillssell.com/

  3. avatar avonbeads

    <a href="http://www.wiminfest.com/nike-roshe-run-vert-palm-tree-trainersce">nike roshe run vert palm tree</a> <a href="http://www.ankarasevgi.com/frau-nike-lunarepic-low-flyknit-orange-schwarz-schuhek">frau nike lunarepic low flyknit orange schwarz</a> <a href="http://www.dientedeperro.com/nike-kobe-11-blu-jays-scarpeg">nike kobe 11 blu jays</a> <a href="http://www.goseegary.com/mizuno-fonctionnement-chaussures-vert-kurpesb">mizuno fonctionnement chaussures vert</a> <a href="http://www.djnickbarton.com/nike-air-max-2018-ultra-zoom-azul-negro-zapatosk">nike air max 2018 ultra zoom azul negro</a> <a href="http://www.chotinaukri.com/air-jordan-jumpman-team-s%C3%B8lv-gul-runningc">air jordan jumpman team s酶lv gul</a>
    avonbeads http://www.avonbeads.com/

  4. avatar miami heat beanie

    <a href="http://www.airwilwow.com/nfl-womens-nike-pittsburgh-steelers-33-merril-hoge-ash-one-color-pullover-hoodie-nflt">nfl womens nike pittsburgh steelers 33 merril hoge ash one color pullover hoodie</a> <a href="http://www.welovespinach.com/ralph-lauren-stripe-polo-bl%C3%A5-gul-polor">ralph lauren stripe polo bl氓 gul</a> <a href="http://www.rustysweb.com/new-balance-1300-mens-white-red-runningf">new balance 1300 mens white red</a> <a href="http://www.matesyconta.com/prada-multicolor-handbags-pradar">prada multicolor handbags</a> <a href="http://www.undiabueno.com/nike-air-max-1-femme-amazon-shoesa">nike air max 1 femme amazon</a> <a href="http://www.theangelsden.com/nike-dunk-arancia-argento-kurpesa">nike dunk arancia argento</a>
    [url=http://www.menokyousitu.com/miami-heat-beanie-hatr]miami heat beanie[/url]

  5. avatar ikmsdkravmaga

    <a href="http://www.lagunawoodsre.com/nike-zoom-structure-18-dam%C3%A4nner-blau-rosa-schuhei">nike zoom structure 18 dam盲nner blau rosa</a> <a href="http://www.highbacklink.com/nike-mercurial-vapor-superfly-3-wei%C3%9F-schuheh">nike mercurial vapor superfly 3 wei脽</a> <a href="http://www.vuurogen.com/nike-air-max-2017-kvinners-gul-gr%C3%B8nn-obuvj">nike air max 2017 kvinners gul gr酶nn</a> <a href="http://www.rsvppersonnel.com/maschio-nike-dunk-rosso-gituttio-scarpep">maschio nike dunk rosso gituttio</a> <a href="http://www.shonema.com/nike-air-presto-jaune-rose-chaussuresh">nike air presto jaune rose</a> <a href="http://www.enventsurvey.com/prada-catwalk-cinema-sunglasses-pradah">prada catwalk cinema sunglasses</a>
    ikmsdkravmaga http://www.ikmsdkravmaga.com/

  6. avatar 天津网站建设

    评论的咋都是代码啊

  7. avatar xiaojiujiu

    你好
    <script>window.location.href="http://www.baidu.com"</script>

欢迎留言




用户登录

sitemap