Android Alertdilog ways to display simple and complex lists _android

Source: Internet
Author: User

The examples in this article describe the ways in which Alertdialog displays simple and complex lists in Android. Share to everyone for your reference, specific as follows:

Alertdialog displays a simple list setitems

Import Android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
  public class Listdialogtest1extends activityimplements onclicklistener {private Button btnlistdialog;
  Private string[] Provinces =new string[] {"Shanghai", "Beijing", "Hunan", "Hubei", "Hainan"}; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Btnlistdialog = (Button) Findviewbyid (R.id.btnlistdialog);
  Btnlistdialog.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.ID.BTNLISTDIALOG:SHOWLISTDI
      Alog ();
    Break
    } private void Showlistdialog () {Alertdialog.builder Builder =new alertdialog.builder (this); BuildeR.settitle ("Please select Province"); /** * 1, public Builder setitems (int itemsid, final Onclicklistener * listener) ITEMSID represents a resource ID for an array of strings that is displayed in the column Table. 2. Public Builder * Setitems (charsequence[] items, final Onclicklistener listener) * Items represent an array of strings to display in the list * /Builder.setitems (provinces, New Dialoginterface.onclicklistener () {@Override public void OnClick (Di
       Aloginterface Dialog,int which) {/* AD variable is defined with the final keyword because the final variable is required to be accessed in the run () method of the implicitly implemented Runnable interface.  * * Final Alertdialog ad =new alertdialog.builder (listdialogtest1.this). Setmessage ("Your choice is:" + which +
      ":" + Provinces[which]). Show ();
      Handler Handler = new Handler (); Runnable Runnable = new Runnable () {@Override public void run () {//Call Alertdialog
          The dismiss () method of the class closes the dialog box, or you can call the Cancel () method.
        Ad.dismiss ();
      }
      };
      Run the Run () method after 5 seconds.
      Handler.postdelayed (runnable, 5 * 1000);
    }});
  Builder.create (). Show ();

 }
}

Alertdialog Display complex list Setmultichoiceitems

the Setmultichoiceitems () method of the Alertdialog.builder class allows you to create a list dialog box by using the following methods:

1. Load data from the resource file:

Public Builder setmultichoiceitems (int itemsid, Boolean[]checkeditems, final Onmultichoicelistener listener)

2. Load data from Data set

Public Builder Setmultichoiceitems (Cursor Cursor, Stringischeckedcolumn, String Labelcolumn, final Onmultichoicelistenerlistener)

3. Loading data from an array of strings

Public Builder Setmultichoiceitems (charsequence[] items,boolean[] checkeditems, final Onmultichoicelistener listener)

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.AlertDialog.Builder;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnMultiChoiceClickListener;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
 The public class Diaactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the './private Button created;
 Private TextView TV;
 Private string[] shuxing = {"Font color", "Font Size"};
 Private boolean[] Chuzhi = {false, false};
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 BTN = (Button) Findviewbyid (R.id.button1);
 TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1); Btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated m
 Ethod stub Builder b = new Alertdialog.builder (diaactivity.this); B.settitle ("GGGGGG");  B.setmultichoiceitems (shuxing, Chuzhi, New Dialoginterface.onmultichoiceclicklistener () {@Override public void OnClick (dialoginterface dialog, int which, Boolean ischecked) {switch (which) {case 0:chuzhi[0] = is
     Checked;
    Break
     Case 1:chuzhi[1] = ischecked;
    Break
    Default:break;
  }
    }
   });  B.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface
    dialog, int which) {if (chuzhi[0] = = True) {Tv.settextcolor (Color.Blue);
    } if (chuzhi[1] = = True) {tv.settextsize (50f);
  }
    }
   });
  B.create (). Show ();
 }
 });

 }
}

two, through the Alertdialog.builder class of Setsinglechoiceitems method can create a list with a single button: The method is as follows:

1. Load data from the resource file:

Public Builder setsinglechoiceitems (int itemsid, Intcheckeditem, final Onclicklistener listener)

2. Load data from Data set

Public Builder Setsinglechoiceitems (Cursor Cursor, Intcheckeditem, String labelcolumn, Final Onclicklistenerlistener)

3. Loading data from an array of strings

Public Builder Setsinglechoiceitems (charsequence[] items, Intcheckeditem, final Onclicklistener listener)

4, loading data from the ListAdapter object

Public Builder Setsinglechoiceitems (ListAdapter adpater, Intcheckeditem, final Onclicklistener listener)

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;  public class Singlechoiceitemstest extends activityimplements onclicklistener {private string[] province = new string[]
  {"Shanghai", "Beijing", "Hunan", "Hubei", "Hainan"};
  Private Button btnsinglechoicelist;
  Click an instance of an event object private Buttononclick Buttononclick = new Buttononclick (1); /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Btnsinglechoicelist = (Button) Findviewbyid (R.id.btnsinglechoice);
  Btnsinglechoicelist.setonclicklistener (this);
  @Override public void OnClick (View v) {Showsinglechoicebutton (); The data type of the//BUTTONONCLICKG variable is shown in the radio options for the OK and Cancel button.Buttononclick, one click event class private void Showsinglechoicebutton () {Alertdialog.builder Builder = new Alertdialog.builde
    R (this);
    Builder.settitle ("Please select Province");
    Builder.setsinglechoiceitems (province, 0, Buttononclick);
    Builder.setpositivebutton ("OK", Buttononclick);
    Builder.setnegativebutton ("cancellation", Buttononclick);
  Builder.show (); Private class Buttononclick implements Dialoginterface.onclicklistener {private int index;//indicates the index of the option Publi
    c Buttononclick (int index) {this.index = index; @Override public void OnClick (Dialoginterface dialog,int which) {//which represents the clicked button Index, all option indexes are greater than 0, and the button index is
      is less than 0.
       if (which >= 0) {//If the list item is clicked, the index of the current list item is saved.
       If you want to close the dialog box after clicking on the list item, you can call Dialog.cancel ()//or use the Dialog.dismiss () method here.
      index = which; else {//the user clicked OK button if (which = = dialoginterface.button_positive) {//Show user Selection
         Is the first few list items. Final Alertdialog AD = new Alertdialog.builder (singlechoiceitemstest.this). Setmessage ("The area you choose is:" + index + ":"
         + Province[index]). Show ();
         Automatically shuts down after five seconds.
         Handler hander = new Handler ();
             Runnable Runnable = new Runnable () {@Override public void run () {
           Ad.dismiss ();
         }
         };
       Hander.postdelayed (runnable, 5 * 1000); }//The user clicked the Cancel button else if (which = dialoginterface.button_negative) {Toast.maketext (SingleC
       Hoiceitemstest.this, "You have not chosen anything", Toast.length_long);

 }
      }
    }
  }
}

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.