Three ways to alert users in Android
Last Update:2015-07-28
Source: Internet
Author: User
<span id="Label3"></p><p><p>I. Toast</p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">Pop up toast Toast.maketext (getapplicationcontext (), "toast", 0). show ();</pre></pre><p><p>Two. Dialog box:<br>1. Confirm the Cancel dialog box</p></p><pre class="brush:java;toolbar: true; auto-links: false;"> public void click1 (view view) { //dialog Box creator alertdialog.builder builder = new builder (this); builder.settitle ("i am a dialog box"); Builder.setmessage ("dialog Box displays"); builder.setpositivebutton ("ok", New onclicklistener () { @Override public void onclick (dialoginterface dialog, int which) { toast.maketext (getApplicationContext (), " OK was clicked ", 0). show (); } }); builder.setnegativebutton ("cancel", new Onclicklistener () &NBSP;{&NBSP;&NBSP;&Nbsp; @Override public void onclick (dialoginterface dialog, int which) { //don't Write Anything. The default implementation is to close the dialog box } }); builder.setcancelable (false); builder.create (). Show (); }</pre><p><p>2. Single-selection dialog box</p></p><pre class="brush:java;toolbar: true; auto-links: false;">Creator of public void Click2 (view view) {//dialog box alertdialog.builder builder = new Builder (this); Builder.settitle ("please Select your gender"); Final string[] items = {"male", "female", "unknown"}; Builder.setsinglechoiceitems (items, 2, New Onclicklistener () {@Override public void OnClick (dialoginterface dia log, int which) {toast.maketext (getapplicationcontext (), "your gender:" +items[which], 0). show (); Dialog.dismiss (); } }); Builder.create (). Show (); }</pre><p><p> <em> 3. </em> multi-select dialog box </p> </p><pre class="brush:java;toolbar: true; auto-links: false;"> public void click3 (view view) { //dialog Box creator alertdialog.builder builder = new builder (this); Builder.settitle ("please Choose your favorite fruit"); final string[] items={"apple", "pear", "pineapple", "banana "," the cucumber "}; final boolean[] result =new boolean[]{true,false, True,false,false}; builder.setmultichoiceitems (items, result, new Onmultichoiceclicklistener () { @Override public void onclick (dialoginterface dialog, int which, boolean IsChecked) { toast.maketext (getapplicationcontext (), items[ Which]+ischecked, 0). Show (); result[which] = ischecked ; } }); builder.setpositivebutton ("submit" , new onclicklistener () { @Override public void onclick (dialoginterface dialog, int which) { stringbuffer sb = new stringbuffer (); for (int i=0;i<result.length;i++) { if (result[i]) { sb.append (items[i]+ ","); } } toast.maketext (getapplicationcontext (), "you have selected," +sb.tostring (), 0). show (); } }); builder.show (); }</pre><p><p>4. Progress Bar dialog box</p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">public void Click4 (view view) {progressdialog pd = new ProgressDialog (this); Pd.settitle ("reminder"); Pd.setmessage ("loading Data ... Please wait a moment. "); Pd.show (); }</pre></pre><p><p>5. Progress Bar dialog box with progress</p></p><pre class="brush:java;toolbar: true; auto-links: false;">Public void click5 (view view) { final progressdialog pd = new progressdialog (this); pd.settitle ("reminder"); Pd.setprogressstyle (progressdialog.style_horizontal); pd.setmax (+); pd.setmessage ("loading Data ... Please wait a moment. "); pd.show (); new thread () { Public void run () { for (int i = 0;i<100;i+ +) { try { Thread.Sleep (; } catch ) (interruptedexception e) { e.printstacktrace (); } pd.setprogress (i); } &nBsp; pd.dismiss (); }; }.start (); }</pre><p><p> Three. notification----android Phone notification bar </p> </p><pre class="brush:java;toolbar: true; auto-links: false;">3.0 Previous versions need to use outdated methods, otherwise error public void click (view view) { notificationmanager nm = (notificationmanager) getsystemservice (NOTIFICATION_SERVICE); notification notification = new notification (R.drawable.notification, "i am a notice", system.currenttimemillis ()); notification.flags = Notification.flag_auto_cancel;//automatically clear notifications after clicking (notification.flag_no_clear can't clear notifications, etc.) Intent Intent = new intent (); intent.setaction (intent.action_call); Intent.setdata (uri.parse ("tel:119"));//call to 119 to increase the access to the phone pendingintent contentintent = pendingintent.getactivity (this, 0, intent, 0); Notification.setlatesteventinfo (this, "i am the title", "i am content", contentintent); Nm.notify (0, notification); } /** * new version of notification * @param view */ @ Suppresslint ("newapi") public void click2 (view view) { Notification noti = new notification.builder (this) .setcontenttitle ("i am the Title") . Setcontenttext ("i am Content") .setsmallicon ( R.drawable.notification) .setlargeicon ( Bitmapfactory.decoderesource (getresources (), r.drawable.ic_launcher)) .build (); notificationmanager nm = ( Notificationmanager) getsystemservice (notification_service); nm.notify (0, noti); }</pre><p><p> Three ways to alert users in Android </p> </p></span>