In the interface design, we need to use the dialog box to display the prompt information, error message, etc. according to the user's operation. There are two ways that the Android implementation tips display is commonly used
1. Toast
2, Alertdialog
A mechanism used in Android to display information that is used very frequently in lightweight messaging development. Its characteristics
1, do not accept user action, no focus
2, the display time is limited, after a certain time will automatically disappear.
Using a toast to display information is straightforward and works as follows:
Toast Toast=toast.maketext (This, "Data loading Complete", toast.length_long);//create toasttoast.show ();//Display message
The common toast looks simple, and we can customize the toast as needed, such as the information tips for collecting and canceling collections in the news headlines, the effect
There are two main aspects of custom toast
1, customize the appearance of toast
2. Set Toast on screen location
Implementing the custom Toast key code is as follows:
Toast toast = toast.maketext (context, MSG, toast.length_long);//create Toastint OffsetX = 0;int OffsetY = 0;toast.setgrav ity (Gravity.bottom, OffsetX, OffsetY);//Set Location LinearLayout ll=layoutinflater.from (context). Inflate (R. Layout.custom_toast,null); Toast.setview (ll);//Set Appearance toast.show ();
The completion code for the above case implementation is as follows:
1,customtoastactivity:
Private Button Btcollect; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview (r.layout.custom_toast_activity); Initview (); } private void Initview () {btcollect= (Button) Super.findviewbyid (r.id.btcollect); Btcollect.setonclicklistener (this); } private void Showtoast (String txt) {Toast tast=toast.maketext (this, TXT, toast.length_long); Tast.setgravity (gravity.center, 0, 0); View View=layoutinflater.from (this). Inflate (r.layout.custom_toast, NULL); TextView tvmsg= (TextView) View.findviewbyid (r.id.tvmsg); Tvmsg.settext (TXT); Tast.setview (view); Tast.show (); public void OnClick (view view) {String txtcollect=btcollect.gettext (). toString (); if (Txtcollect.equals ("collection")) {Btcollect.settext ("Cancel Collection"); Showtoast ("successful Collection"); }else{Btcollect.settext ("collection"); Showtoast ("Cancel collectionPossession "); } }
2 , Custom_toast.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= " Http://schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:padding=" 10DP "Android:la yout_height= "Match_parent" android:background= "@drawable/toast_shape" > <textview android:id= "@+id/tvImg" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "60SP" android:text= "★" android:textcolor= "@color/white" android:layout_centerhorizontal= "true" > </TextView> <textview android:id= "@+id/tvmsg" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize= "28sp" android:text= "collection succeeded" android:textcolor= "@color/white" android:layout_below= "@+id/tvimg" android:layout_centerhorizontal= "true"/></rel Ativelayout>
3, Toast_shape.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <solid android:color= "#000000" ></solid> <corners android:radius= "10DP"/></ Shape>
1, Alertdialog
A simple dialog box, the main purpose is to display a warning message to the user, through Alertdialog.builder to generate Alertdialog, the main code is as follows:
Dialog Dialog = new Alertdialog.builder (this) . Settitle ("dialog box") //Set caption . Setmessage ("displays the prompt message. ") //Display information . SetIcon (r.drawable.pic_m) //Set the displayed Iconsetpositivebutton (" OK ", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) { Dialog.dismiss () ; }}) Set button. Create (); Create Dialogdialog.show (); Show dialog box
Other main methods:
Setview: Setting Custom styles for dialog boxes
Setitems: A list of Settings dialog boxes to display, typically used to display several commands
Setsinglechoiceitems: Used to set a dialog box to display a series of radio boxes
Setmultichoiceitems: Used to set the dialog box to display a series of check boxes
Setneutralbutton: Normal button
Setpositivebutton: Add "Yes" button to the dialog box
Setnegativebutton: Dialog box Add "No" button
Create: Creating dialog box
Show: Show dialog box
2, Custom Alertdialog
Mainly use GetWindow (). Setcontentview () to set the appearance, such as Implementing the Exit Application dialog box
Activity--customalertdialog Code:
private void Showexitdialog () {final Alertdialog exitdlg=new Alertdialog.builder (this ). Create (); Exitdlg.show ();//execute Window Win=exitdlg.getwindow () before adding content; Set the content page of the window to define the view content Win.setcontentview (r.layout.exit_dlg_layout) for the exit_dlg_layout.xml file; TextView tvmsg= (TextView) Win.findviewbyid (r.id.tvmsg); Tvmsg.settext ("Do you want to quit the app?") "); Button btok= (button) Win.findviewbyid (R.id.btok); Button btcancel= (button) Win.findviewbyid (R.id.btcancel); To add an event to the Confirm button, perform an exit application Operation Btcancel.setonclicklistener (new View.onclicklistener () {public void OnClick (View View) {Exitdlg.dismiss ();//Close Dialog}}); Btok.setonclicklistener (New View.onclicklistener () {public void OnClick (View arg0) {finish (); Exitdlg.dismiss ();//Close Dialog}}); } public void onbackpressed () {//press the fallback key to invoke Showexitdialog (); }
exit_dlg_ Layout.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:padding=" 10DP "> < LinearLayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation= " Vertical "android:background=" @drawable/exit_dlg_shape "android:padding=" 5DP "> <textview Andro Id:layout_width= "Match_parent" android:layout_height= "30DP" android:gravity= "center" android:textsize = "20SP" android:text= "warm Tip" android:textcolor= "#000000"/> <linearlayout android:layout_width= "m Atch_parent "android:layout_height=" 100DP "android:orientation=" vertical "android:background=" #ffffff "> & Lt TextView android:id= "@+id/tvmsg" android:layout_width= "Match_parent" Android oid:layout_height= "Match_parent" Android:gravity= "center_vertical" android:padding= "5DP"/> </LinearLayout> &l T LinearLayout android:layout_width= "match_parent" android:padding= "5DP" Android:layout_h eight= "40DP" android:orientation= "horizontal" > <button android:id= "@+id/btok" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "OK" android:layout_weight= "1" android:background= "@drawable/comm_shop_detail_top_green" android:layout_marginright= "2DP"/> <button android:id= "@+id/btcance L "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:text= "Cancel" android:layout_weight= "1" android:background= "@drawable/comm_shop_detail_t Op_black " /> </LinearLayout> </LinearLayout></RelativeLayout>
Android Interface Design dialog box--Custom toast, Alertdialog