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

2016年04月28日 Android 15条评论 阅读5129次

对话框在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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

15 条评论

  1. <a href="http://www.laketreepress.com/air-jordan-retro-14-femminile-rosa-cielo-blu-scarpel">air jordan retro 14 femminile rosa cielo blu</a> <a href="http://www.prohaarklinik.com/nike-air-force-1-low-womens-white-shoesj">nike air force 1 low womens white</a> <a href="http://www.bonfanticare.com/herre-asics-gel-sendai-2-bl%C3%A5-lyser%C3%B8d-skoi">herre asics gel sendai 2 bl氓 lyser酶d</a> <a href="http://www.antoniopagni.com/nike-lunarglide-6-femminile-blu-arancia-scarpeq">nike lunarglide 6 femminile blu arancia</a> <a href="http://www.wcshosting.com/adidas-damian-lillard-black-white-shoesj">adidas damian lillard black white</a> <a href="http://www.biddingstream.com/michael-kors-bedford-large-leather-shoulder-mkh">michael kors bedford large leather shoulder</a>
    thecampingman http://www.thecampingman.com/

  2. <a href="http://www.lasdamas.net/hombres-asics-gel-noosa-tri-8-negro-verde-butyc">hombres asics gel noosa tri 8 negro verde</a> <a href="http://www.lavoilebleue.net/adidas-zx-flux-triple-noir-queen-obuve">adidas zx flux triple noir queen</a> <a href="http://www.lifechurchindy.net/nike-huarache-kvinders-hvid-nikes-obuvf">nike huarache kvinders hvid nikes</a> <a href="http://www.lincei.net/nike-free-run-2-hommes-bon-march%C3%A9-uk-kurpese">nike free run 2 hommes bon march茅 uk</a> <a href="http://www.litpower.net/air-jordan-retro-1-enfants-gris-or-schuhee">air jordan retro 1 enfants gris or</a> <a href="http://www.lngstation.net/nike-dunk-hi-skinny-nero-oro-leopard-kurpesf">nike dunk hi skinny nero oro leopard</a>
    amishcraftedgazebos http://www.amishcraftedgazebos.net/

  3. <a href="http://www.laketreepress.com/prada-fringe-backpack-pradah">prada fringe backpack</a> <a href="http://www.prohaarklinik.com/kam-chancellor-seattle-seahawks-jersey-nfli">kam chancellor seattle seahawks jersey</a> <a href="http://www.bonfanticare.com/kvinders-nike-zoom-structure-18-appelsin-gr%C3%A5-skol">kvinders nike zoom structure 18 appelsin gr氓</a> <a href="http://www.antoniopagni.com/long-sleeve-sports-polo-shirts-poloh">long sleeve sports polo shirts</a> <a href="http://www.wcshosting.com/mens-nike-roshe-run-hyp-pink-gold-shoesi">mens nike roshe run hyp pink gold</a> <a href="http://www.biddingstream.com/nike-free-tr-womens-black-green-shoesp">nike free tr womens black green</a>
    grossavind http://www.grossavind.com/

  4. <a href="http://www.cardiachits.com/dam%C3%A4nner-air-jordan-retro-4-gold-gr%C3%BCn-schuheh">dam盲nner air jordan retro 4 gold gr眉n</a> <a href="http://www.zestchannel.com/kvinners-air-jordan-retro-13-bl%C3%A5-gr%C3%B8nn-obuvj">kvinners air jordan retro 13 bl氓 gr酶nn</a> <a href="http://www.eczacinim.com/adidas-soccers-scarpe-cielo-blu-gituttio-scarpek">adidas soccers scarpe cielo blu gituttio</a> <a href="http://www.chilangorails.com/femmes-air-jordan-retro-2-gris-rose-chaussuresh">femmes air jordan retro 2 gris rose</a> <a href="http://www.mariusen.com/prada-card-holder-wallet-pradah">prada card holder wallet</a> <a href="http://www.brittonoilco.com/stephen-curry-jersey-female-nfli">stephen curry jersey female</a>
    mintyschoice http://www.mintyschoice.com/

  5. avatar ankarasanal

    <a href="http://www.twistnik.com/georgia-bulldogs-top-of-the-world-ncaa-grype-stretch-cap-hatj">georgia bulldogs top of the world ncaa grype stretch cap</a> <a href="http://www.pkbocrangsu.com/nike-mercurial-vapor-x-rosa-nero-scarpej">nike mercurial vapor x rosa nero</a> <a href="http://www.celudela.com/adidas-ace-15.1-fg-ag-blau-schwarz-gelb-schuhei">adidas ace 15.1 fg ag blau schwarz gelb</a> <a href="http://www.fooksung.com/kvinder-adidas-zx-flux-sort-hvid-skoh">kvinder adidas zx flux sort hvid</a> <a href="http://www.ankaramarti.com/vintage-nashville-predators-jersey-nfli">vintage nashville predators jersey</a> <a href="http://www.gcpoloclub.com/mennns-reebok-realflex-gr%C3%B8nn-obuvk">mennns reebok realflex gr酶nn</a>
    ankarasanal http://www.ankarasanal.com/

  6. avatar 天津网站建设

    评论的咋都是代码啊

  7. avatar nvusforex

    <a href="http://www.cheapcoatsfr.com/kobe-10-elite-svart-and-gull-scarpeb">kobe 10 elite svart and gull</a> <a href="http://www.mylinkstv.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.phxfam.com/nike-air-flight-89-lila-gr%C3%BCn-kurpesb">nike air flight 89 lila gr眉n</a> <a href="http://www.shanibazaar.com/adidas-adizero-counterblast-7-zapatost">adidas adizero counterblast 7</a> <a href="http://www.imannequins.com/custom-painted-converse-uk-shoesz">custom painted converse uk</a> <a href="http://www.irentevent.com/nike-air-max-1-vert-gris-violet-schuheb">nike air max 1 vert gris violet</a>
    nvusforex http://www.nvusforex.com/

  8. avatar annomaplexel

    <a href="http://www.acforlessllc.com/nike-roshe-run-iv-gr%C3%B8nn-gul-chaussuresa">nike roshe run iv gr酶nn gul</a> <a href="http://www.thehodostore.com/nba-jerseys-milwaukee-bucks-11-monta-ellis-white-nba-jerseys-nflg">nba jerseys milwaukee bucks 11 monta ellis white nba jerseys</a> <a href="http://www.gandoff.com/new-balance-420-womens-yellow-black-runningg">new balance 420 womens yellow black</a> <a href="http://www.langbenlang.com/women-2012-new-nfl-jerseys-san-francisco-49ers-53-navorro-bowman-red-jerseys-hall-of-fames-50th-patch-nflv">women 2012 new nfl jerseys san francisco 49ers 53 navorro bowman red jerseys hall of fames 50th patch</a> <a href="http://www.glennloovens.com/air-jordan-7-really-black-gray-trainersa">air jordan 7 really black gray</a> <a href="http://www.kamilotomotiv.com/nike-free-run-2-prm-ext-silber-trainers-game-scarpec">nike free run 2 prm ext silber trainers game</a>
    annomaplexel http://www.annomaplexel.com/

  9. avatar 9992ulhr

    <a href="http://www.suopbhi0.com/air-jordan-1-mid-baskets-butye">air jordan 1 mid baskets</a> <a href="http://www.rpus144x.com/hommes-asics-gel-kinsei-4-bleu-ciel-violet-butya">hommes asics gel kinsei 4 bleu ciel violet</a> <a href="http://www.064eaffu.com/herre-salomon-gcs-athletic-trail-r%C3%B8d-lyser%C3%B8d-obuvb">herre salomon gcs athletic trail r酶d lyser酶d</a> <a href="http://www.2g9s16lv.com/nike-blazer-alle-schwarz-leder-scarpec">nike blazer alle schwarz leder</a> <a href="http://www.waxhq23s.com/femminile-new-balance-420-royal-blu-schuhef">femminile new balance 420 royal blu</a> <a href="http://www.2pdyrpwy.com/nike-classic-cortez-fonctionnement-chaussures-scarpec">nike classic cortez fonctionnement chaussures</a>
    9992ulhr http://www.9992ulhr.com/

  10. avatar gpsgolfcaddy

    <a href="http://www.solotes.net/herre-nike-air-huarache-light-sort-r%C3%B8d-obuva">herre nike air huarache light sort r酶d</a> <a href="http://www.squidsolutions.net/unisex-nike-roshe-run-s%C3%B8lv-lyser%C3%B8d-schuhee">unisex nike roshe run s酶lv lyser酶d</a> <a href="http://www.stbankarena.net/nike-lebron-witness-negro-gris-chaussuresg">nike lebron witness negro gris</a> <a href="http://www.streakmedia.net/nike-internationalist-graphics-herre-sko-obuvc">nike internationalist graphics herre sko</a> <a href="http://www.superseals.net/nike-lunartempo-all-svart-guy-kurpesb">nike lunartempo all svart guy</a> <a href="http://www.svjusdreaming.net/air-jordan-retro-7-mujeres-plata-blanco-kurpese">air jordan retro 7 mujeres plata blanco</a>
    gpsgolfcaddy http://www.gpsgolfcaddy.net/

  11. avatar lbclubmitu

    <a href="http://www.emanmoon.com/nike-flyknit-lunar-3-hyper-jade-skos">nike flyknit lunar 3 hyper jade</a> <a href="http://www.mysiatravel.com/limited-benjarvus-greenellis-jersey-cincinnati-bengals-42-home-black-nflg">limited benjarvus greenellis jersey cincinnati bengals 42 home black</a> <a href="http://www.pandoraboxhn.com/bon-march%C3%A9-nike-sb-blazer-high-hommes-or-skoe">bon march茅 nike sb blazer high hommes or</a> <a href="http://www.yourctt.com/polo-ralph-lauren-cashmere-blend-scarf-scarfr">polo ralph lauren cashmere blend scarf</a> <a href="http://www.coccodesigns.com/air-max-light-release-sneakerb">air max light release</a> <a href="http://www.lucasdow.com/billig-adidas-superstar-kvinners-oransje-shoesd">billig adidas superstar kvinners oransje</a>
    lbclubmitu http://www.lbclubmitu.com/

  12. avatar elyseprather

    <a href="http://www.polurnetsucks.com/nike-huarache-tout-noir-acheter-schuheg">nike huarache tout noir acheter</a> <a href="http://www.taskanizerapp.com/jordan-11-legend-azul-aaa-scarpeg">jordan 11 legend azul aaa</a> <a href="http://www.balloonpunkt.com/nike-kobe-vert-rose-kurpesf">nike kobe vert rose</a> <a href="http://www.biereaveyron.com/nike-blazer-gr%C3%A5-sail-schuheg">nike blazer gr氓 sail</a> <a href="http://www.churchatsea.com/nike-air-huarache-grigio-and-viola-schuheb">nike air huarache grigio and viola</a> <a href="http://www.esmokecanada.com/air-jordan-retro-11-armada-azul-chaussurese">air jordan retro 11 armada azul</a>
    elyseprather http://www.elyseprather.com/

  13. avatar sotainvalidi

    <a href="http://www.itlooksohot.com/silver-black-womens-nike-shox-shoes-sneakere">silver black womens nike shox shoes</a> <a href="http://www.isiklardamper.com/elite-mike-brown-jersey-jacksonville-jaguars-12-alternate-black-nfld">elite mike brown jersey jacksonville jaguars 12 alternate black</a> <a href="http://www.phihochdrei.com/nike-toki-slip-txt-herre-guld-shoesc">nike toki slip txt herre guld</a> <a href="http://www.firmayoga.com/jordan-1-black-gum-jordan-laser-skof">jordan 1 black gum jordan laser</a> <a href="http://www.rayyanstore.com/nike-free-3.0-v4-womens-light-green-urine-sportse">nike free 3.0 v4 womens light green urine</a> <a href="http://www.faithboxwomen.com/adidas-nmd-runner-dam%C3%A4nner-rot-runningd">adidas nmd runner dam盲nner rot</a>
    [url=http://www.sotainvalidi.com/]sotainvalidi[/url]

  14. avatar smartpaul

    <a href="http://www.royalhoneywax.com/michael-kors-bedford-belted-handbag-mkr">michael kors bedford belted handbag</a> <a href="http://www.carslambert.com/barato-new-balance-890-mujeres-naranja-sneakere">barato new balance 890 mujeres naranja</a> <a href="http://www.hiussalipiilo.com/game-ryan-kerrigan-washington-redskins-youth-road-jersey-91-white-nflb">game ryan kerrigan washington redskins youth road jersey 91 white</a> <a href="http://www.hospicevna.com/adidas-zx-630-herre-hvid-shoesc">adidas zx 630 herre hvid</a> <a href="http://www.smartpaul.com/billig-nike-free-5.0-v3-kvinners-r%C3%B8d-skog">billig nike free 5.0 v3 kvinners r酶d</a> <a href="http://www.opendekalbinc.com/yeezy-boost-750-for-sale-quad-cities-sportsf">yeezy boost 750 for sale quad cities</a>
    smartpaul http://www.smartpaul.com/

  15. avatar xiaojiujiu

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

欢迎留言




用户登录

sitemap