General dialog box, Radio Dialog box, multi-select Dialog box, dialog with icon, and custom adapter and custom View dialog in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>dialog box is a alertdialog, but a simple alertdialog, we can play it out a lot of tricks, below we come together to summarize the use of Alertdialog. Look at the Children's shoes in peacetime work has used the Alertdialog these Characteristics.</p></p><p><p>OK, nonsense not much to say, into our today's topic.</p></p>Normal dialog box<p><p>Ordinary dialog box is our most commonly used dialog box, the implementation is not complicated, the implementation of the effect is of course the simplest, as Follows:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_1_6129047" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_1_6129047" name="code" class="java">Alertdialog dialog = new Alertdialog.builder (this). settitle ("normal Dialog box"). SetIcon (r.drawable.a4s). Setnegativebutton ("cancel ", null). Setpositivebutton (" OK ", new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {//handle Confirmation Button's Click event}}). Setneutralbutton ("neutral", null). setmessage ("confirm delete?" "). Create ();d ialog.show ();</pre></pre><br><br><p><p></p></p><p><p>We can set the title of a dialog box, we can set the icon, at the bottom of the dialog we can set three buttons, respectively, is canceled, ok, neutral, in general, we can add the corresponding click event in the confirmation Button. OK, So what does it look like in a dialog box? As follows:<br><br></p></p>dialog box with item displayed<p><p>If we want to display several item in our dialog box, we can set the item to Alertdialog as Follows:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_2_7840914" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_2_7840914" name="code" class="java">Alertdialog dialog = new Alertdialog.builder (this). settitle ("show Item Dialog box"). SetIcon (r.drawable.a4s). setitems (items, New Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {toast.maketext ( mainactivity.this, items[which], toast.length_short). Show ();}}). Create ();d ialog.show ();</pre></pre><br>The items here are an array of type string, as Follows:<p><p></p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_3_5158719" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_3_5158719" name="code" class="java">Items = new string[] {"wei", "shu", "wu"};</pre></pre><br>So here we have no need to set the bottom three buttons, directly click on the item, trigger the corresponding Event. In the OnClick method there is an int parameter called which, which indicates that the user clicked on the item, showing the effect as Follows:<p><p></p></p><p><p></p></p><p><p>When the user clicks on a different item, we make the corresponding processing in the OnClick method according to the which Value.</p></p>Single-selection dialog box<p><p>Like the dialog box that displays the item, the Radio dialog box does not really need to set the bottom three buttons (if you want to set it ok), then his code is this:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_4_8680729" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_4_8680729" name="code" class="java">Alertdialog dialog = new Alertdialog.builder (this). settitle ("radio Dialog box"). SetIcon (r.drawable.a4s). setsinglechoiceitems ( items,-1, new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {toast.maketext ( mainactivity.this, items[which], toast.length_short). show ();d Ialog.dismiss ();}). Create ();d ialog.show ();</pre></pre><br>This time we call the method is Setsinglechoiceitems this method, this method has three parameters, where the value of items is the same as the previous one, the second parameter represents the preselected item, the third parameter is the event processing Method. The event handling method is the same, the difference is that there is a sentence Dialog.dismiss (); yes, Here we have to manually turn off Dialog. Then it shows the effect of this:<p><p></p></p><p><p></p></p><p><p>When we select any item, the dialog will be automatically closed, and the corresponding Click event is processed in the onclick method.</p></p>Multi-select dialog box<br><p><p>Unlike the radio dialog box, the Multi-select dialog requires that we have the following button, then here I will set two buttons below, let's look at the code:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_5_5475146" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_5_5475146" name="code" class="java">Alertdialog dialog = new Alertdialog.builder (this). settitle ("multiple selection dialog"). setIcon (r.drawable.a4s). Setnegativebutton ("cancel ", null). Setpositivebutton (" ok ", null). setmultichoiceitems (items, null, new Onmultichoiceclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which, boolean isChecked) {}}). create ();d ialog.show ();</pre></pre><br>Like setsinglechoiceitems, the setmultichoiceitems here has three parameters, namely the data source, the item in the preselection, and the Item's click event, and the item in the preselection is a Boolean array. Indicates which item is selected at the beginning and which item is not selected, and the Click event here also has a parameter, that is, ischecked, indicates whether the item is selected, then we can record the Item's selection by a Boolean array. I'm not going to write it alone Here. The multi-select dialog appears as Shown:<p><p></p></p><p><p><br></p></p><p><p>Once we've chosen each of our options, Click OK to get the corresponding Data.</p></p>Custom Adapter<br><p><p>Some students may have noticed, builder also has a method called setadapter, the parameter of this method is a listadapter, is we usually make for the ListView adapter, Can set adapter means that our alertdialog can show the content more rich, then here I use the simplest arrayadapter to do a demo for everyone, the code is as Follows:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_6_5616850" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_6_5616850" name="code" class="java">adapter = new Arrayadapter<string> (this, android. r.layout.simple_list_item_1, new string[] {"zhang san", "john doe"}); Alertdialog dialog = new Alertdialog.builder (this). settitle ("custom adapter"). setIcon (r.drawable.a4s). setadapter ( adapter, new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int which) {}}). Create (); Dialog.show ();</pre></pre><br>The display results are as follows:<p><p></p></p><p><p></p></p><p><p>This time we want to show alertdialog what it looks like, adapter gives us a lot of Flexibility.</p></p>Custom View<br><p><p>of course, the ultimate Big Boss here is not a custom adapter, but a custom view, through custom view, we can let our alertdialog display any interface we want, the code is as Follows:</p></p><p><p></p></p><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_7_4363114" name="code" class="java"><pre code_snippet_id="1580145" snippet_file_name="blog_20160216_7_4363114" name="code" class="java">View view = Layoutinflater.from (this). Inflate (r.layout.activity_main, null); Alertdialog dialog = new Alertdialog.builder (this). settitle ("custom view"). setIcon (r.drawable.a4s). setview (view). Create ();d ialog.show ();</pre></pre><br>The display results are as follows:<p><p></p></p><p><p></p></p><p><p>Using custom view, We can customize a variety of dialog out, such as login dialog, about or description of dialog and so On.</p></p><p><p><span style="font-size:12px;">Alertdialog Use the way basically so many, demo download <span style="font-size:18px;">http://download.csdn.net/detail/u012702547/9433640</span></span><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>General dialog box, Radio Dialog box, multi-select Dialog box, dialog with icon, and custom adapter and custom View dialog in Android</p></p></span>

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.