Android Toast custom and prevent repeated display, Android toast

Source: Internet
Author: User

Android Toast custom and prevent repeated display, Android toast

Toast is a kind of reminder for the user in the Android system when the user misoperations or when a function is executed, it has no focus and will disappear within a certain period of time, however, when a user attempts to perform a wrong operation (such as a wrong password during logon) multiple times, multiple Toast instances are created. The system puts these toast instances into the queue and waits until the previous Toast is displayed, next, the user will see multiple Toast prompts. Whether you exit the software or not, this will greatly compromise the user experience, so what we need to do is, if Toast is already displayed (that is, Toast! = Null), you do not need to refresh the new information. You can directly setText to display the information. Only when Toast is empty Can you refresh the new information and analyze it here, you should understand how to write it. The second problem is that in order to make Toast match our own application style, we often need to customize Toast display. Next, we will solve these two problems:

Since it is a custom layout, a custom layout is often displayed, and the custom background -- custom_toast.xml is added:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "35dp" android: background = "@ drawable/toast_bg" android: padding = "10dp"> <TextView android: id = "@ + id/toast_message" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: gravity = "center" android: maxLines = "2" android: textColor = "@ color/text" android: textSize = "14sp" android: paddingLeft = "2dp" android: paddingRight = "2dp" android: text = "network connection failed"/> </RelativeLayout>


Next, let's look at the main code:

Import android. content. context; import android. OS. handler; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. widget. textView; import android. widget. toast;/***** @ author byl */public class ToastUtil {private static Toast mToast; private static Handler mHandler = new Handler (); private static Runnable r = new Runnable () {public void run () {mToast. cancel (); mToast = null; // hide toast and set it to null }}; public static void show1_toast (Context context, String message) {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); View view = inflater. inflate (R. layout. custom_toast, null); // custom Layout TextView text = (TextView) view. findViewById (R. id. toast_message); // displays the prompt text. setText (message); mHandler. removeCallbacks (r); if (mToast = null) {// re-create only when mToast = null; otherwise, you only need to change the prompt text mToast = new Toast (context); mToast. setDuration (Toast. LENGTH_SHORT); mToast. setGravity (Gravity. BOTTOM, 0,150); mToast. setView (view);} mHandler. postDelayed (r, 1000); // toastmToast is hidden for 1 second. show ();}}


You can call the showdesktoast () method directly, for example, ToastUtil. showdesktoast (this, "the password cannot be blank "),


The result is as follows:




In android development, I wrote the following two lines so that toast does not repeat. Why not?

Your Toast is not an object. Is it estimated that you will only execute else content?
The Toast. makeText Example statement is not a method for calling the Toast object ~ Because you don't have a new Toast object, do you?

This call method is actually to call the static method of the Toast class (static Keyword Method), and no new object is created.

Your idea is not to repeat Toast, so you can define a Toast object globally, and then you can determine ~~
However, toast = null should not work .. The cancel method just disappears when toast appears. It does not empty the toast object, so let's modify it ~~ You can define a boolean to judge it ~~

How does android toast customize the display duration?

Yes. Use the reflection mechanism to forcibly fix the display time.
 

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.