How to define Toast and androidtoast in Android

Source: Internet
Author: User

How to define Toast and androidtoast in Android

There is something in the Android system that is very useful and commonly used, that isToastHowever, if you use Toast for a long time, you will find that Toast has its own shortcomings: the form is single, only the text and style remain unchanged.Customize a ToastWe can start by analyzing the code that defines Toast for Android:

The makeText method of Toast:




In fact, what Android does is set the text and duration displayed by Toast, and thenThe Toast object is returned.To execute the show () method. The core point here is to understand


What does Android do? Let's continue to look at it:



Here is actuallyA new TN () object is added, and some location and offset parameters are set for the TN object.. So where is TN holy? We can continue to look:




We can find that the TN constructor executesHandleShow Method




We can find that the handleshow method executes a lot of statements, but in fact we only need to pay attention to one sentence, that is, the one selected above:MWM. addView (mView, mParams );


In fact, this mWM is a WindowManager, and mView is the view of the component to be displayed. mParams should be set as a parameter, but where is it holy? We can find a member variable in the Toast class.:


We found that mParams is actually a layout parameter of WindowManager. However, if the mParams settings are still too messy from the handleShow method alone, we can find the following section in Toast::




Here we can clearly see some of the main parameter settings of mParams, including the length and width of which are wrap_content, TransLucent, and an animation effect (Animation_Toast ), and a title (setTitle ("Toast") finally set Toast some identification, such as making the screen light, do not touch, do not get focus and so on.


Here, we basically know how Toast is displayed. The core is to use the addView method of WindowManager to transmit the view and parameter mParams to be displayed, however, some mParams parameters need to be set before this;


So here is the display,ToHow does ast make the display disappear?What about it? We can findHandHide Method:




The core statement is also selected,MWM. removeView (mView), so that Toast disappears


With some of the above analysis, we can easily write our own Toast class, which provides some static methods for us to define the display style and content of Toast, you can also control the start time and end time of Toast display.

The following custom Toast class provides several show methods. You can pass in text to display a testview like a traditional Toast, or you can define the input view object and mParams parameters by yourself, in this way, a fully defined Toast can be defined.


Package com. alexchen. mobilesafeexercise. ui; import android. content. context; import android. graphics. pixelFormat; import android. view. view; import android. view. windowManager; import android. widget. textView; import android. widget. toast;/*** custom Toast ** @ author Alex **/public class MyToast {/*** form manager */private static WindowManager wm; private static WindowManager. layoutParams mParams = new WindowManager. la YoutParams (); private static View mView; private static TextView TV; /*** display custom toast ** @ param info * @ param context */public static void show (String message, Context context) {wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); TV = new TextView (context); TV. setText (message); TV. setTextSize (20); // the work originally done by TN WindowManager. layoutParams params = mParams; params. height = WindowManager. layou TParams. WRAP_CONTENT; params. width = WindowManager. layoutParams. WRAP_CONTENT; params. format = PixelFormat. TRANSLUCENT; params. type = WindowManager. layoutParams. TYPE_TOAST; params. setTitle ("Toast"); params. flags = WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_TOUCHABLE; wm. addView (TV, params);}/*** customize the display method of text toast. Multi-user-defined content, for example, textview can apply an input style id ** @ param message * @ param context * @ param textViewResid * @ param params */public static void show (String message, Context context, int textViewResid, WindowManager. layoutParams params) {wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); TV = new TextView (context); // adds a style TV. setTextAppearance (context, textViewResid); // the work originally done by TN // WindowMan Ager. layoutParams params = mParams; // params. height = WindowManager. layoutParams. WRAP_CONTENT; // params. width = WindowManager. layoutParams. WRAP_CONTENT; // params. format = PixelFormat. TRANSLUCENT; // params. type = WindowManager. layoutParams. TYPE_TOAST; // params. setTitle ("Toast"); // params. flags = WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON // | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE // | Win DowManager. layoutParams. FLAG_NOT_TOUCHABLE; wm. addView (TV, params);}/*** more custom formats, you can directly input any custom view, set the wm parameter ** @ param view * @ param context * @ param params * WindowManager. layoutParams type parameter, WindowManager. layoutParams * mParams = new WindowManager. layoutParams (); params. height = * WindowManager. layoutParams. WRAP_CONTENT; params. width = * WindowManager. layoutParams. WRAP_CONTENT; params. format = * PixelFormat. TRANSLUCENT; params. type = * WindowManager. layoutParams. TYPE_TOAST; * params. setTitle ("Toast"); params. flags = * WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON | * WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | * WindowManager. layoutParams. FLAG_NOT_TOUCHABLE; */public static void show (View view, Context context, WindowManager. layoutParams params) {wm = (WindowManager) context. getSy StemService (Context. WINDOW_SERVICE); MyToast. mView = view; wm. addView (mView, params);}/*** hide custom toast. Remember to leave it empty because when you do not call, these two quantities should be empty */public static void hide () {if (wm! = Null) {if (TV! = Null) {wm. removeView (TV); TV = null;} if (mView! = Null) {wm. removeView (mView); mView = null;} if (TV = null & mView = null) {wm = null ;}}}}




How does android toast customize the display duration?

There are only two modes: long and shot ..
 
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.