Android 018 Dialog & Toast & Notification & Menu

Source: Internet
Author: User
Tags unique id


One, a Toast display message prompt box

The

Toast class is used to display a message prompt box on the screen that does not have any control buttons, and
does not get the focus, disappears automatically after a certain amount of time, and is typically used to display some quick hints, not the
frequently used
Syntax:
1, Context context = Getapplicationcontext ();
Charsequence Text = "Hello toast!";
int duration =toast.length_short;
Toast Toast =toast.maketext (Context, text, duration);
Toast.show ();
Toast Toast=new Toast (this);
Toast Toast=toast.maketext (This, "What to display", toast.length_short);
Toast.show ();
Note: Calling the show () method of the Toast   class displays a message prompt, and it is important to note that this method is called,
otherwise the Set Message prompt box will not be displayed.
Common methods:
Method   Description
setduration (int duration)   used to set the duration of the message prompt, usually using
Toast.length_long or Toa St. The Length_short parameter value
setgravity (int gravity,int xoffset,int
Yoffset)
is used to set the position of the message hint box, and the parameter gravity is used to specify the
alignment. Xoffset and Yoffset are used to specify a specific offset value
SetMargin (float
horizontalmargin,float
VerticalMargin)
To set the margin for the message hint
SetText (charsequence s)   used to set the text content to be displayed
Setview (view view)   To set the view that will be displayed in the message prompt
:

Second, the use of Alertdialog to implement the dialog Alertdialog class function is very powerful, it can not only generate a button with the prompt dialog box, you can also generate a column
Table List dialog box, use Alertdialog to generate a dialog box that summarizes four types of
1, with OK, neutral and cancel three buttons of the prompt dialog box, where the number of buttons is not fixed, you can according to the need
To add.
2, List with List dialog box
3, List dialog box with multiple single-selection list items and N buttons
4, List dialog box with multiple select list items and N buttons
Common methods:
Method description
Settitle (charsequence title) to set the caption for the dialog box
SetIcon (drawable icon) to set the icon for the dialog box
SetIcon (int resId) to set the icon for the dialog box
Setmessage (charsequence
Message
Used to set what to display for the prompt dialog box
Setbutton ()
Used to add a button to a hint dialog box, which can be a Cancel button, neutral press
Knob the OK button. You need to specify the int type for the
The Whichbutton parameter is implemented, and its parameter value can be
Dialoginterface.button_positive (OK button),
Button_negative (Cancel button) or button_neutral
(Neutral button)
Typically, you can only generate a hint dialog box with N buttons using the Alertdialog class to generate another three lists
dialog box, you need to use the Alertdialog.builder class, and the Alertdialog.builder class provides the following common methods:
Method description
Settitle (charsequence title) to set the caption for the dialog box
SetIcon (drawable icon) to set the icon for the dialog box
SetIcon (int resId) to set the icon for the dialog box
Setmessage (charsequence
Message
Used to set what to display for the prompt dialog box
Setnegativebutton () To add a Cancel button to a dialog box
Setpositivebutton () to add a OK button to a dialog box
Setneutralbutton () To add a neutral button to a dialog box
Setitems () to add list items to a dialog box
Setsinglechoiceitems () to add a single-selection list item to a dialog box
Setmultichoiceitems () to add multiple-selection list items to a dialog box
Setview () set a custom layout for the popup box
Case
Third, use Notification to display the notification definition on the status bar:
A control that displays instant information, which is displayed in the title bar, and you can see the full style of the notification when you pull it
Notification and Notificationmanager. To handle the push information of the status bar, using both to send a display
Four steps to know
Call the Getsystemservice () method to get the system's Notificationmanager service
Create a Notification object and set the properties of it
To set event information for a Notification object
Send Notification notification via notify () of the Notificationmanager class
Through Notificationmanager manager.cancel (notifyid_1); You can clear the notification
3.1 General Notice
(1) The mandatory option to use normal notifications:
① Add a caption, use the Setcontenttitle () method
② settings icon, use the Setsmallicon () method
③ add content, use the Setcontenttext () method
(2) Options available
①setdefaults (System default Ringtone | system default Vibration)
②setticker (Sets the contents of the title bar to be displayed, typically ContentText)
(3) Steps to create a notification
① Defining Notificationcompat.builder Objects
② settings must have options and optional
(4) Release notice
Notificationmanager.notify (id,notification).
id→ each notification has a unique ID
notification→ notifies the object, not the Notificationcompat.builder object, by invoking the
The Builder.build () method gets
(5) Click event
① using Pendingintent, the method is as follows
Getactivity (context context, int requestcode, Intent Intent, int flags)
Context: Obtaining a Contextual object
Requestcode: Get a return code for another Activity
Intent:pendingintent events that need to be triggered, such as jump Activity
Flags: Notification Flag bit
The ② uses Setcontentintent (pendingintent) to encapsulate the pendingintent
Like to put it in this method
Intent intent=new Intent (mainactivity.this,contentactivity.class);
Pendingintent
Pendingintent=pendingintent.getactivity (mainactivity.this, 0, intent, 0);
Builder.setcontentintent (pendingintent);
3.2 Large View notifications
Note: Large views are not supported for versions under 4.1
① first create a simple notification
Notificationcompat.builder Mbuilder = new Notificationcompat.builder (this);
Mbuilder.setsmallicon (R.drawable.alerm);
Mbuilder.setcontenttitle ("Event tracker");
Mbuilder.setcontenttext ("Events received");
② use the Notificationcompat.inboxstyle object to set the style of the large view notification, with the following code:
Notificationcompat.inboxstyle Inboxstyle =new
Notificationcompat.inboxstyle ();
Set a large view title
Inboxstyle.setbigcontenttitle ("Event Tracker Details:");
Add event content to a large view
for (int i=0; i < 5; i++) {
Inboxstyle.addline ("Details:" +i);
}
③ use the SetStyle method to put the Inboxstyle object in
Mbuilder.setstyle (Inboxstyle);
1.notificationcompat.inboxstyle Inbox Style: Display multiple lines of text
2.notificationcompat.bigpicturestyle big Picture Style: detail area contains a bit of 256DP height
Figure
3.notificationcompat.bigtextstyle Large Text style: Displays a large block of text
3.3 Progress bar Notification
When downloading, a progress bar notification is displayed in the notification bar
① Create a regular notification
② open a thread that uses setprogress (the maximum value of the progress value, the current progress value, false) in the thread, and the Notify
method to notify the progress bar immediately, the code is as follows:
Mbuilder.setprogress (Incr,false);
Mnotifymanager.notify (0, Mbuilder.build ());
3.4 Custom Notifications
① Create a regular notification
② Create a custom layout file with the suffix XML
③ creates an Remoteviews object that loads the XML layout into the object, with the following code:
Remoteviews content = new Remoteviews (Getpackagename (), R.layout.item);
④ using the SetContent (content) method to load Remoteviews objects into a normal notification object
Notification.builder Builder = new Notification.builder (this);
Builder.setsmallicon (R.drawable.ic_launcher);
Builder.setcontenttitle ("Custom");
Remoteviews views = new Remoteviews (Getpackagename (), r.layout.activity_content);
Builder.setcontent (views);
Manager.notify (1, builder.getnotification ());
Four, the use of Menus menu definition: When the user click on the Phone menu button will pop-up menu, menu in 2.3 system display up to 6 items, in the 4.0 system
The number of items in the menu depends on the height of the screen.
2.3 In
4.0 in
Optionsmenu:
(1) in the code:
① Rewrite public boolean oncreateoptionsmenu (Menu menu) {...} Method
② creating menu items: Menu.add (0, 1, 5, "one");
The first parameter is groupid,group to group menu items
The second argument is ItemID, which refers to the ID of the menu item
The third argument is Order, which specifies the order of the menu item's position
The fourth parameter is title, which refers to the text displayed by the menu item.
Note: A return value of TRUE indicates that the system-supplied menu is called when the user presses the menu key, and false is called
Customize the menu.
③ Listener
Overriding the Onoptionsitemselected method
(2) In XML:
①xml file Location: Res/menu folder
② notation
<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/new_game"
android:icon= "@drawable/ic_new_game"
android:title= "@string/new_game"
android:showasaction= "Ifroom"/>
<item android:id= "@+id/help"
android:icon= "@drawable/ic_help"
android:title= "@string/help"/>
</menu>
A. To create a menu XML, you must use menu as the root tag
B.item Labels as menu items
C.android:id indicates the ID of item
D.android:icon represents the menu icon, 4.0 system only displays the icon on the ActionBar
E.android:title represents the text of a menu
F.showasaction Indicates whether the item is displayed on the ActionBar, and only 4.0 of the system can display
※ Sub-Menu
<?xml version= "1.0" encoding= "Utf-8"?>
<menuxmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/file"
android:title= "@string/file" >
<!--"File" submenu--
<menu>
<item android:id= "@+id/create_new"
android:title= "@string/create_new"/>
<item android:id= "@+id/open"
android:title= "@string/open"/>
</menu>
</item>
</menu>
Note: item must be a double-label, placing the Menu submenu in the Item tab
③ introduction of XML in Activity
@Override
Publicboolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.xxx, menu);
Returntrue;
}
Context Menu
1. Override method:
public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
Menu.clear ();//Clear the ContextMenu of the system, mainly for the 2.3.x system
if (v = = Findviewbyid (r.id.edittext1)) {
Menu.setheadericon (R.drawable.ic_launcher);//Add an icon to the context menu
Menu.setheadertitle ("gender");//Add a title to the context menu
Menu.add (0, 0, 0, "male");//Set menu item, same as Optionsmenu
Menu.add (0, 1, 0, "female");
}
}
2. Click events:
public boolean oncontextitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case 0:
Et.settext (Item.gettitle ());
Break
Case 1:
Et.settext (Item.gettitle ());
Break
}
return true;
}
3. Registration:
This.registerforcontextmenu (need to display the ContextMenu control);














Android 018 Dialog & Toast & Notification & Menu

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.