Message prompt mechanism
A dialog box Dialog Alertdialog
dialog box: Standard dialog box, Menu dialog box, menu-Style check box dialog box, custom dialog box
(Note: Here the context is common, the code of the click Method I have been in the click on the Listener event registered)
1 Standard dialog box:
Write the button in the layout interface, then set the Click event,
The constructor for creating dialog in event type Alertdialog.builder builder = new Alertdialog.builder (context);
Then use the constructor to set the various properties of the dialog, and finally create,show out. The code is as follows:
public void ShowDialog () {Alertdialog.builder Builder = new Alertdialog.builder ( context); Builder.seticon (r.drawable.g_18). Settitle (R.string.dialog_title). Setmessage (R.string.isdelete). Setcancelable (False). Setpositivebutton (R.string.button_ok,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog,int which) {Toast.maketext (context, which + "", Toast.length_short);}}) Sets whether the dialog box can be interrupted, that is, clicking on an activity's other interface without displaying a dialog box. Setnegativebutton (r.string.button_cancel,new Dialoginterface.onclicklistener () {//Cancel button @overridepublic void OnClick (Dialoginterface dialog,int which) { Dialog.dismiss (); Exit the dialog box}). Create (). Show ();
2 menu-Type dialog public void Menudialog () {final string[] Rolearray = Getresources (). Getstringarray (R.array.array_role);//Get array number According to Alertdialog.builder builder = new Builder (context); Builder.seticon (r.drawable.png_0019)//Settings icon. Settitle ("QQ Chinese character"). Setitems (r.array.array_role,new Dialoginterface.onclicklistener () {//Set ListView item data and add Click event Listener @overridepublic Void OnClick (Dialoginterface dialog,int which) {toastutils.show (context, Rolearray[which]);/////display the obtained value}}). Create (). Show ( );}
3 Menu-Style check box dialog box
Here is a sparse Boolean array to use this array or to get the option you clicked, there are key,value;
Key: The location of the item you clicked on is position, (note: This location is not the index position of the array).
Value: A Boolean value of TRUE indicates that you have selected this checkbox,false.
public void Menucheckboxdialog () {string[] Lovecityarray = Getresources (). Getstringarray (r.array.array_lovecity); Alertdialog.builder builder = new Builder (context), Builder.seticon (r.drawable.png_city) Settitle ("which cities have you been to?"). "). Setmultichoiceitems (R.array.array_lovecity, Null,new Dialoginterface.onmultichoiceclicklistener () {@ overridepublic void OnClick (Dialoginterface dialog,int which, Boolean isChecked) {/** Alertdialog alertdialog= ( Alertdialog) * Dialog; Transformation down String str=* alertdialog.getlistview* (). Getadapter (). GetItem (which). toString (); *// Gets the data for each item in a ListView (object) * toastutils.show (context, str); */})//OK button to get the item selected by the user. Setpositivebutton ( R.string.button_ok,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog,int which) {Alertdialog Alertdialog = (alertdialog) dialog; ListView listview= Alertdialog.getlistview ();//Gets the ListView, gets the sparse Boolean array of this array key Valuesparsebooleanarray Sparseboleanarray=alertdialog.getlistview (). getcheckeditempositions (); int size= sparSeboleanarray.size (); StringBuilder sb=new StringBuilder (); for (int i=0;i<size;i++) {if (Sparseboleanarray.valueat (i)) {int position= Sparseboleanarray.keyat (i); String selectstr = Listview.getadapter (). GetItem (position). toString (); Sb.append (SELECTSTR);}} Toastutils.show (Context, sb.tostring ());}) Cancel button. Setnegativebutton (r.string.button_cancel,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog,int which) {Dialog.dismiss ();//Exit Dialog}). Create (). Show ();
4 Customize dialog box
Here I did not use the system theme, but myself wrote a style (style and theme not discussed here).
By default there will be a system-provided background, and I have written a custom style to inherit the system-provided style, overriding the system's background properties
<style name= "Mydiy_dialog" parent= "@android: Style/theme.dialog" >
<item name= "Android:windowbackground" > @android:color/transparent</item>
</style>
Implementation ideas:
Create a layout XML, which is the layout that displays my dialog box.
Creates a dialog object that loads the window layout XML.
Child control text edit box found through parent control dialog
Setting Up Listener Events
public void Diydialog () {
Final Dialog dialog=new Dialog (context,r.style.mydiy_dialog); Creating a Window Object
Dialog.setcontentview (R.layout.diydialog); To load the custom dialog form into a dialog box
Final EditText et= (EditText) Dialog.findviewbyid (R.id.txt_et); Find child control text edit box by paying control
Button btn_ok= (button) Dialog.findviewbyid (R.ID.OK_BTN); Find the OK button
Button btn_cancel= (button) Dialog.findviewbyid (R.ID.CANCLE_BTN); Find the Cancel button
Btn_ok.setonclicklistener (New Onclicklistener () {//Set OK button for Click Listen Event
@Override
public void OnClick (View v) {
String Streidttext=et.gettext (). toString (). Trim (); Gets the text value of the EditText control
Dialog.dismiss ();
//.... Other logic
Toastutils.show (context, streidttext); Toast Help class display text values
}
});
Btn_cancel.setonclicklistener (New Onclicklistener () {//Setting the Cancel button's Listen event
@Override
public void OnClick (View v) {
Dialog.dismiss ();
}
});
Dialog.show ();
}
Two Logcat: Log cat
1 levels: V, D, I, W, e level from low to high
-Verbose: Confusing, all information can be output, the lowest level
-Debug: Debug Level
-Info: Information level
-Warning: Warning level
-Error: Wrong level
2 adding filters to filter by tag
3 The difference between the System.out.println () and the log output
-SYSTEM.OUT.PRINTLN () output information level is info level, tag tag:System.out
-System.out.print () output information requires System.out.flush () Brush Cache output
Three toast: Toast
A toast notification is a message reminder that pops up on top of the screen, filling only the space needed for the message, while the currently running activity retains its own visibility and interactivity.
This notification automatically fades in and does not accept interactive events.
1 XML definition Toast
Define the layout in XML and then write the Java code.
public void DiyToast2 (View v) {
Defining Toast Objects
Toast toast = new toast (context);
Layout filler
/**
* Instantiate the layout XML file as an object and populate the contents of the layout into the View object
* Resource: Layout resource root: parent control or root node
* Inflate: The form of an air cylinder fills a layout resource into a View object with a hierarchy of view objects
*/
View view = Layoutinflater.from (context). Inflate (R.layout.diy, NULL);
Toast.setview (view);
Set duration
Toast.setduration (Toast.length_long);
Gravity: Displayed position, Xoffset: offset Yoffset: Offset
Toast.setgravity (gravity.left, 0, 0);
Toast.show ();
}
2 Customizing toasts in the form of code
public void DiyToast1 (View v) {
Declaring a Toast object
Toast toast = new toast (context);
Building Layout Objects
LinearLayout layout = new LinearLayout (context);
Viewgroup.layoutparams params = new Layoutparams (
Layoutparams.match_parent, layoutparams.wrap_content);
Setting Layout parameters
Layout.setlayoutparams (params);
Set the direction of a linear layout
Layout.setorientation (linearlayout.horizontal);
Layout.setbackgroundcolor (Color.cyan);
Creating an Image Object
ImageView image = new ImageView (context);
Image.setimageresource (r.drawable.emoji_044);
To add a picture view control to a layout object
Layout.addview (image);
Building a text view control
TextView message = new TextView (context);
Message.settext ("Brazil World Cup!");
Message.settextcolor (Color.Blue);
Layout.addview (message);
Set the view in the Toast object
Toast.setview (layout);
Set duration
Toast.setduration (Toast.length_long);
Gravity: Display position, xoffset:x direction offset yoffset:y direction offset
Toast.setgravity (gravity.left, 0, 0);
Toast.show ();}
Android message prompt mechanism