<span id="Label3"></p>Android Learning note 20 Toast toast, notification notification, Popupwindow popup toast Toasts<p><p>Toast Toast is a control that we often use, a toast is a mechanism used by Androidos to display messages, Unlike dialog, where toasts do not get to the focus, usually show up automatically after a period of time, let's describe several common ways of toast:</p></p><p><p>The first, the default display mode, is also the most common way:</p></p><pre><pre><code> Toast.makeText(MainActivity.this, "这是默认的显示方式", Toast.LENGTH_SHORT).show();</code></pre></pre><p><p>second, Customize how the settings are displayed:</p></p><pre><pre><code>Toast toast = Toast.makeText(MainActivity.this, "这是自定义显示位置的效果", Toast.LENGTH_SHORT);toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER, 0, 0);toast.show();</code></pre></pre><p><p>third, with picture display:</p></p><pre><pre><code> Toast toast = Toast.makeText(MainActivity.this, "这是带图片的效果", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER, 0, 0);LinearLayout linearLayout = (LinearLayout) toast.getView();ImageView imageView = new ImageView(MainActivity.this);imageView.setImageResource(R.mipmap.ic_launcher);linearLayout.addView(imageView, 0);toast.show();</code></pre></pre><p><p>The fourth type, fully customizable display mode:</p></p><pre><pre><code> LayoutInflater layoutInflater = getLayoutInflater();View toastView = layoutInflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.ll_toast));Toast toast = new Toast(MainActivity.this);toast.setDuration(Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);toast.setView(toastView);toast.show();</code></pre></pre><p><p>Layout file Code:</p></p><pre><pre><code><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_toast"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="16sp"android:layout_marginTop="16sp"android:gravity="center"android:orientation="horizontal"><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是完全自定义的吐司" android:textSize="20sp" /></LinearLayout></code></pre></pre><p><p>fifth, other threads show through Handler:</p></p><pre><pre><code>new Thread(new Runnable() {@Overridepublic void run() {handler.sendEmptyMessage(1);}}).start();public void showToast() { Toast.makeText(MainActivity.this, "这是来自其它线程的吐司", Toast.LENGTH_SHORT).show();}private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { showToast(); } super.handleMessage(msg); }};</code></pre></pre><p><p>Realize:</p></p><p><p></p></p><p><p>Poke Download Source code</p></p><p><p>Finally attach the domestic mirroring API for toast</p></p>Notification Notice<p><p>Notification is a global effect notification that programs generally send notification through the Notificationmanager service. Before AndroidAPI3.0, it is recommended to build a message using notification.builder, but after 3.0, it is recommended to use Notificationcompat.builder BUILD.</p></p><p><p>Here we introduce the basic use of notification:</p></p><p><p>Notifications mainly involve two classes:</p></p> <ul> <ul> <li>Notification Class: notification Information class, which is used to host the content of the notification, generally we do not directly build this object, instead, it uses an inner class of Notificationcompat.builder to instantiate an object (using Notification.builder under Android3.0) and to set various properties of the notification, finally through Notificationcompat.build The Er.build () method gets a notification Object. Once you have this object, you can use the Notificationmanager.notify () method to send Notifications.</li> <li>Notificationmanager Class: is a notification Manager class, This object is a service maintained by the system, is obtained in a singleton mode, so it is not generally directly instantiated this Object. In activity, we directly pass in Context.notification_service to get the Notificationmanager object by calling the Activity.getsystemservice (String) method.</li> </ul> </ul><p><p>Related methods for notification settings:</p></p> <ul> <li><li>Setcontenttitle (charsequence): Sets the caption that must be set</li></li> <li><li>Setcontenttext (charsequence): set content that must be set</li></li> <li><li>Setsmallicon (int): Sets the small icon that appears at the top of the notification when it is received and must be set</li></li> <li><li>Setsubtext (charsequence): Sets the text of a small line below the content</li></li> <li><li>Setticker (charsequence): Sets the text message to be displayed at the top when the notification is received</li></li> <li><li>Setlargeicon (Bitmap): Set Large icon</li></li> <li><li>Setautocancel (boolean): Whether the user clicks Notification Click on the panel to let the notification cancel (default is not canceled)</li></li> <li><li>SetDefaults (int): The simplest to add a sound, strobe, and vibration effect to a notification, using the default (defaults) property, you can combine multiple properties, notification.default_vibrate (add a default vibrate reminder) ; notification.default_sound (add default sound reminder); notification.default_lights (add default Tri-color light alert); notification.default_all ( Add default of 3 all reminders Above)</li></li> <li><li>Setvibrate (long[]): Set Vibration mode</li></li> <li><li>setlights (int argb, int onms, int offms): set three-color lights, parameters are: light color, light duration, dark Time</li></li> <li><li>Setsound (Uri): Set the ringtone when receiving the notification, can use the system, or you can set the</li></li> <li><li>Setongoing (boolean): set to Ture to indicate that it is an in-progress notification</li></li> <li><li>Setprogress (int,int,boolean): set the notification parameters with the progress bar in Order: the maximum value of the progress bar, the current progress, whether the progress is uncertain if the progress bar is determined</li></li> <li><li>Setcontentintent (pendingintent): pendingintent and intent slightly different, it can set the number of executions, mainly used for remote service communication, alarm, notification, launcher, sms, in general, with less</li></li> <li><li>SetPriority (int): Set Priority</li></li> </ul><p><p>Steps to use Notification:</p></p> <ul> <ul> <li>First step: get a notificationmanager object,</li> <li>Step two: Create a builder construction class for the notification bar</li> <li>The third step: the relevant settings for the builder, by using the relevant methods described above</li> <li>Fourth step: call Builder's build () method to assign a value of notification</li> <li>Fifth Step: call Notificationmanager's notify () method to send a notification</li> </ul> </ul><p><p>Note: the Android system offers two ways to cancel notifications: one is to notification own maintenance, use the Setautocancel () method to set whether to maintain, pass a Boolean type of Data. Another way to maintain the Notificationmanager Notification Manager object is to use cancel (int) to remove a specified notification when it sends a notification through notify (), specifying the notification ID to operate the Notification. You can also use Cancelall () to remove all notifications.</p></p><p><p>Let's experience the use of notification by implementing a simple example:</p></p><pre><code>@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mcontext = mainactivity.this; Btn_default_notification = (Button) Findviewbyid (r.id.btn_default_notification); Btn_default_notification.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (View View) {Intent Intent = new Intent (mcontext, defaultactivity.class); Pendingintent pendingintent = pendingintent.getactivity (mcontext, 0, intent, 0); Notificationmanager Notificationmanager = (notificationmanager) MainActivity.this.getSystemService (notification_ SERVICE); Notification.builder Builder = new Notification.builder (mainactivity.this); Builder.setsmallicon (r.mipmap.ic_launcher); Builder.setcontenttitle ("default Message notification"); Builder.setcontenttext ("i am The default display message, I came in"); Builder.setwhen (system.currenttimemillis ()); BUilder.setticker ("receive a message"); Builder.setdefaults (notification.default_lights | notification.default_vibrate); Builder.setautocancel (true); Builder.setcontentintent (pendingintent); If (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES. Jelly_bean) {Notification Notification = Builder.build (); Notificationmanager.notify (NOTIFICATION, NOTIFICATION); } } });}</code></pre><p><p>In this case, just put Java code, the others are no longer set up, but also relatively simple.</p></p><p><p>Poke Download Source code</p></p><p><p>As usual, attach Notification's domestic mirroring API</p></p>Popupwindow pop-up window<p><p>Popupwindow This class is used to implement a popup box, Popupwindow can load any content view, the popup is suspended above the current Activity. If Popupwindow shows, then all the events will be intercepted, except home, for example: a popupwindow pop-up, then you need to click on the Phone's exit button to let Popupwindow disappear, and then the activity will Exit.</p></p><p><p>Let's take a look at the Popupwindow with an example:</p></p><p><p>Activity code:</p></p><pre><code>Package Com.example.popupwindow;import Android.graphics.drawable.colordrawable;import Android.os.Bundle;import Android.support.v7.app.appcompatactivity;import Android.view.layoutinflater;import android.view.MotionEvent; Import Android.view.view;import Android.view.viewgroup;import Android.widget.button;import Android.widget.popupwindow;import Android.widget.toast;public class Mainactivity extends Appcompatactivity {private Button btn_show_pop; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (r.layout.activity_main); Btn_show_pop = (Button) Findviewbyid (r.id.btn_show_pop); Btn_show_pop.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (view View) { Initpop (view); } });} private void Initpop (view view) {view popview = layoutinflater.from (mainactivity.this). Inflate (r.layout.pop_custom, nu ll); Button btn_01 = (button) Popview.findviewbyid (r.id.btn_01); Button btn_02 = (button) Popview.findviewbyid (r.id.btn_02); Button btn_03 = (button) Popview.findviewbyid (r.id.btn_03); Initializes a popupwindow, the first parameter is the loaded view, the second parameter is the width of the view, the third parameter is the height of the view, and the fourth is whether to get the focus final Popupwindow Popupwindow = new Popupwindow (popview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); Set Load animation Popupwindow.setanimationstyle (r.anim.pop_anim); Settings can be clicked in the margin to let Popupwindow disappear popupwindow.setoutsidetouchable (true); Popupwindow.settouchable (true); Popupwindow.settouchinterceptor (new View.ontouchlistener () {@Override public boolean onTouch (view view, Mot Ionevent Motionevent) {System.out.println ("touch"); If this returns true, the touch event will be intercepted to return false; } }); Sets the background color popupwindow.setbackgrounddrawable (new colordrawable (0x00ff0000)); Set the position of the Popupwindow display, the first parameter is the relative View, the second is the X axis, the third parameter is the Y-axis//showasdropdown (View anchor): position relative to a control (just below the left), no offset// Showasdropdown (View Anchor, int xoff, int yoff): relative to the position of a control, there is an offset//showatlocation (View parent, int gravity, int x, int y): The position relative to the parent control (for example, the central gravity.c ENTER, below gravity.bottom, etc.), You can set the offset or no offset ps:parent This parameter as long as the activity of the view is possible! Popupwindow.showasdropdown (view, 40, 0); Btn_01.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (view View) { Toast.maketext (mainactivity.this, "you clicked on the fruit", toast.length_short). show (); Popupwindow.dismiss (); } }); Btn_02.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (view View) { Toast.maketext (mainactivity.this, "you clicked the sport", toast.length_short). show (); Popupwindow.dismiss (); } }); Btn_03.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (view View) { Toast.maketext (mainactivity.this, "you clicked to eat", toast.length_short). show (); Popupwindow.dismiss (); } });}}</code></pre><p><p>Layout file Code:</p></p><pre><pre><code><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Button android:id="@+id/btn_01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="水果" /><Button android:id="@+id/btn_02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="运动" /><Button android:id="@+id/btn_03" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="吃饭" /></LinearLayout></code></pre></pre><p><p>Implementation Results:</p></p><p><p></p></p><p><p>of these, Popupwindow has 9 methods of construction, as shown in:</p></p><p><p></p></p><p><p>Here are a few things we use:</p></p><pre><pre><code>public PopupWindow (Context context)public PopupWindow(View contentView, int width, int height)public PopupWindow(View contentView)public PopupWindow(View contentView, int width, int height, boolean focusable)</code></pre></pre><p><p>Poke Download Demo source code</p></p><p><p>The introduction of the pop-up window is here, and finally, as usual, attach popupwindow's domestic Mirror API</p></p> <p><p>Android Learning note 20 Toast toast, notification notification, popupwindow pop-up window</p></p></span>
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