The implementation method of nine kinds of dialog box for Android development _android

Source: Internet
Author: User
Tags int size set background stub

In the development process, with the user interactive will inevitably use the dialog box to achieve a better user experience, so mastering several dialog box implementation method is very necessary. Make a brief introduction to Alertdialog before looking at specific examples. Alertdialog is the most feature-rich, Practice-wide dialog box that generates a variety of content dialogs. But in fact the Alertdialog generated dialog box can be divided into the following 4 areas: Icon area, title area, content area, button area.

Here is a summary of the implementation of nine kinds of dialog boxes, the need for friends to learn under the

In addition to Popupwindow to achieve a little trouble, the other shape is relatively simple, familiar with the 2 can be

directly on the source

Package com.naoh.stu; 
Import java.util.ArrayList; 
Import android.app.Activity; 
Import Android.app.AlertDialog; 
Import Android.app.ProgressDialog; 
Import Android.content.DialogInterface; 
Import android.graphics.drawable.BitmapDrawable; 
Import Android.os.Bundle; 
Import android.view.Gravity; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.ViewGroup.LayoutParams; 
Import Android.view.WindowManager; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.PopupWindow; 
Import Android.widget.Toast;  
  public class Diaallactivity extends activity implements Runnable {private Button btn_dianormal; 
  Private Button Btn_diamulti; 
  Private Button btn_dialist; 
  Private Button Btn_diasinchos; 
  Private Button Btn_diamultichos; 
  Private Button btn_diaprocess; 
  Private Button btn_diareadprocess; 
  Private Button Btn_diacustom; 
  Private Button Btn_popupdia; PrivatePopupwindow Window=null; 
  Private Button cusPopupBtn1; 
  Private View Popupview; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.dialog); 
    GetView (); 
  Setlistener (); 
    private void GetView () {btn_dianormal= (Button) Findviewbyid (r.id.btn_dianormal); 
    Btn_diamulti= (Button) Findviewbyid (R.id.btn_diamulti); 
    Btn_dialist= (Button) Findviewbyid (r.id.btn_dialist); 
    Btn_diasinchos= (Button) Findviewbyid (R.id.btn_diasigchos); 
    Btn_diamultichos= (Button) Findviewbyid (R.id.btn_diamultichos); 
    Btn_diaprocess= (Button) Findviewbyid (r.id.btn_diaprocess); 
    Btn_diareadprocess= (Button) Findviewbyid (r.id.btn_diareadprocess); 
    Btn_diacustom= (Button) Findviewbyid (R.id.btn_diacustom); 
  Btn_popupdia= (Button) Findviewbyid (R.id.btn_popupdia); 
    private void Setlistener () {Btn_dianormal.setonclicklistener (Btnlistener); Btn_diamulti.setonclicklistener (BtnlisTener); 
    Btn_dialist.setonclicklistener (Btnlistener); 
    Btn_diasinchos.setonclicklistener (Btnlistener); 
    Btn_diamultichos.setonclicklistener (Btnlistener); 
    Btn_diaprocess.setonclicklistener (Btnlistener); 
    Btn_diareadprocess.setonclicklistener (Btnlistener); 
    Btn_diacustom.setonclicklistener (Btnlistener); 
  Btn_popupdia.setonclicklistener (Btnlistener);  
      Private Button.onclicklistener btnlistener= New Button.onclicklistener () {public void OnClick (View v) { 
        if (v instanceof Button) {int Btnid=v.getid (); 
            Switch (Btnid) {case R.id.btn_dianormal:shownormaldia (); 
          Break 
            Case R.id.btn_diamulti:showmultidia (); 
          Break 
            Case R.id.btn_dialist:showlistdia (); 
          Break 
            Case R.id.btn_diasigchos:showsinchosdia (); 
          Break 
           Case R.id.btn_diamultichos: Showmultichosdia (); 
          Break 
            Case R.id.btn_diareadprocess:showreadprocess (); 
          Break 
            Case R.id.btn_diaprocess:showprocessdia (); 
          Break 
            Case R.id.btn_diacustom:showcustomdia (); 
          Break 
            Case R.id.btn_popupdia:showcuspopup (v); 
          Break 
        Default:break; 
  } 
      } 
    } 
  }; /* Common dialog box */private void Shownormaldia () {//alertdialog.builder normaldialog=new alertdialog.builder (getapplica 
    Tioncontext ()); 
    Alertdialog.builder normaldia=new Alertdialog.builder (diaallactivity.this); 
    Normaldia.seticon (R.drawable.ic_launcher); 
    Normaldia.settitle ("Ordinary dialog box"); 
    Normaldia.setmessage ("Message contents of the ordinary dialog box"); Normaldia.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dial Oginterface dialog, int which) {//TODO auto-generated method Stub showclickmessage ("OK"); 
    } 
    }); Normaldia.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dial 
      Oginterface dialog, int which) {//TODO auto-generated Method Stub showclickmessage ("Cancel"); 
    } 
    }); 
  Normaldia.create (). Show (); }/* Multi-button dialog box */private void Showmultidia () {alertdialog.builder multidia=new alertdialog.builder (diaallactivi 
    Ty.this); 
    Multidia.settitle ("Multi-Option dialog box"); Multidia.setpositivebutton ("button One", New Dialoginterface.onclicklistener () {@Override public void OnClick (Dial 
      Oginterface dialog, int which) {//TODO auto-generated Method Stub showclickmessage ("button One"); 
    } 
    }); Multidia.setneutralbutton ("button Two", New Dialoginterface.onclicklistener () {@Override public void OnClick (Dialo Ginterface dialog, int which) {//TODO auto-generated method stub showClickmessage ("button two"); 
    } 
    }); Multidia.setnegativebutton ("button three", New Dialoginterface.onclicklistener () {@Override public void OnClick (Dial 
      Oginterface dialog, int which) {//TODO auto-generated Method Stub showclickmessage ("button three"); 
    } 
    }); 
  Multidia.create (). Show (); 
    /* List dialog/private void Showlistdia () {final string[] mlist={"option 1", "Option 2", "option 3", "Option 4", "option 5", "option 6", "option 7"}; 
    Alertdialog.builder listdia=new Alertdialog.builder (diaallactivity.this); 
    Listdia.settitle ("List dialog box"); Listdia.setitems (Mlist, New Dialoginterface.onclicklistener () {@Override public void OnClick (DIALOGINTERFAC e dialog, int which) {//TODO auto-generated Method Stub/* subscript is a/showclickmessage starting from 0 * (mlist[w 
      Hich]); 
    } 
    }); 
  Listdia.create (). Show (); 
  }/* Single selection dialog box/int yourchose=-1; private void Showsinchosdia () {final string[] mlist={"option 1", "Option 2", "option 3", "Option 4", "option 5""," option 6 "," option 7 "}; 
    Yourchose=-1; 
    Alertdialog.builder sinchosdia=new Alertdialog.builder (diaallactivity.this); 
    Sinchosdia.settitle ("Single selection dialog box"); Sinchosdia.setsinglechoiceitems (mlist, 0, New Dialoginterface.onclicklistener () {@Override public void Oncl 
      Ick (dialoginterface dialog, int which) {//TODO auto-generated method stub Yourchose=which; 
    } 
    }); Sinchosdia.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dia 
          Loginterface dialog, int which) {//TODO auto-generated Method stub if (yourchose!=-1) { 
        Showclickmessage (Mlist[yourchose]); 
    } 
      } 
    }); 
  Sinchosdia.create (). Show (); 
  } arraylist<integer> mychose= new arraylist<integer> (); 
    private void Showmultichosdia () {final string[] mlist={"option 1", "Option 2", "option 3", "Option 4", "option 5", "option 6", "option 7"}; Final Boolean Mchosests[]={false,false,false,falSe,false,false,false}; 
    Mychose.clear (); 
    Alertdialog.builder multichosdia=new Alertdialog.builder (diaallactivity.this); 
    Multichosdia.settitle ("Multiple selection dialog box"); 
      Multichosdia.setmultichoiceitems (Mlist, mchosests, New Dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which, Boolean ischecked) {//TODO auto-generated method Stu 
        b if (ischecked) {mychose.add (which); 
        else {mychose.remove (which); 
    } 
      } 
    }); Multichosdia.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (D 
        Ialoginterface dialog, int which) {//TODO auto-generated method Stub int size=mychose.size (); 
        String str= ""; 
        for (int i=0;i<size;i++) {str+=mlist[mychose.get (i)]; 
      } showclickmessage (str); 
    } 
    });Multichosdia.create (). Show (); 
  The//Progress reading box needs to simulate reading ProgressDialog mreadprocessdia=null; 
  Public final static int max_readprocess = 100; 
    private void Showreadprocess () {mreadprocessdia=new progressdialog (diaallactivity.this); 
    Mreadprocessdia.setprogress (0); 
    Mreadprocessdia.settitle ("Progress bar Window"); 
    Mreadprocessdia.setprogressstyle (progressdialog.style_horizontal); 
    Mreadprocessdia.setmax (max_readprocess); 
    Mreadprocessdia.show (); 
  New Thread (This). Start (); 
    //A new thread is opened, the loop accumulates until 100 and then stops @Override public void run () {int progress= 0; 
        while (Progress < max_readprocess) {try {thread.sleep (100); 
        progress++; 
      Mreadprocessdia.incrementprogressby (1); 
      catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); 
  }//read out after the window disappears from Mreadprocessdia.cancel (); 
    /* The dialog box in read/* private void Showprocessdia () {ProgressDialog processdia= New ProgressDialog (diaallactivity.this); 
    Processdia.settitle ("Progress bar Box"); 
    Processdia.setmessage ("content reads ..."); 
    Processdia.setindeterminate (TRUE); 
    Processdia.setcancelable (TRUE); 
  Processdia.show (); /* Custom Dialog box * * private void Showcustomdia () {alertdialog.builder customdia=new alertdialog.builder (diaallacti 
    Vity.this); 
    Final View Viewdia=layoutinflater.from (diaallactivity.this). Inflate (r.layout.custom_dialog, NULL); 
    Customdia.settitle ("Custom dialog box"); 
    Customdia.setview (Viewdia); Customdia.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dial Oginterface dialog, int which) {//TODO auto-generated method Stub edittext diainput= (EditText) Viewdia 
        . Findviewbyid (R.id.txt_cusdiainput); 
      Showclickmessage (Diainput.gettext (). toString ()); 
    } 
    }); 
  Customdia.create (). Show (); /*popup window to implement the */private void SHOWCUSPOPUp (View parent) {if (window = = null) {Popupview=layoutinflater.from (diaallactivity.this). Inflate (R.L 
      Ayout.dia_cuspopup_dia, NULL); 
      cuspopupbtn1= (Button) Popupview.findviewbyid (r.id.diacuspopupsure); 
    Window =new Popupwindow (popupview,layoutparams.fill_parent,layoutparams.fill_parent); 
    } window.setanimationstyle (R.style.popupanimation); /* The setbackgrounddrawable must be invoked because Popupwindow detects whether the background is null at the initial time and if Ontouch or OnKey events do not. So you have to set background*//* Online There are also many people say, pop pop, do not respond to keyboard events, this is in fact the focus of the pop inside the view went. 
    */Window.setfocusable (TRUE);  
    Window.setbackgrounddrawable (New bitmapdrawable ()); 
    Window.update (); 
    Window.showatlocation (parent, gravity.center_vertical, 0, 0); Cuspopupbtn1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {// 
      TODO auto-generated Method Stub showclickmessage ("OK for popup window"); 
  } 
    }); /* Show clicked content/private void Showclickmessage (String message) {Toast.maketext (Diaallactivity.this, "You chose:" +message, Toast.length_short). Sho 
  W ();  } 
}

layout, which is a bunch of button

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G" 
  T <textview android:layout_width= "fill_parent" android:layout_height= wrap_content "android:text=" various dialog Collection "/> <button android:layout_width=" fill_parent android:layout_height= "Wrap_content" Android:te xt= "Ordinary Dialog" android:id= "@+id/btn_dianormal"/> <button android:layout_width= "Fill_parent" Androi  
    d:layout_height= "wrap_content" android:text= "multiple button Dialog" android:id= "@+id/btn_diamulti"/> <button Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "List Dialog" android:id= 
    "@+id/btn_dialist"/> <button android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Single Selection dialOG "android:id=" @+id/btn_diasigchos "/> <button android:layout_width=" Fill_parent "Android:layout_ height= "wrap_content" android:text= "Multiple Choice Dialog" android:id= "@+id/btn_diamultichos"/> <button Oid:layout_width= "Fill_parent" android:layout_height= wrap_content "android:text=" Progress bar Dialog "Android:id=" @+ Id/btn_diareadprocess "/> <button android:layout_width=" fill_parent "android:layout_height=" Wrap_conten Dialog "android:id=" @+id/btn_diaprocess "/> <button android:layout_width=" fill in T "android:text=" reads  
  _parent "android:layout_height=" wrap_content "android:text=" Custom Dialog "android:id=" @+id/btn_diacustom "/>" <button android:layout_width= "fill_parent" android:layout_height= wrap_content "android:text=" PopUp  Window Implementation Dialog "Android:id=" @+id/btn_popupdia/> </LinearLayout>

Above is the Android development must know nine kinds of dialog box implementation method, the code mainly uses the Alertdialog to generate the dialog box, its function is rich, uses the widest.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.