Optimization of android project with toast prompt

Source: Internet
Author: User

Optimization of android project with toast prompt

During login verification, we often use toast to prompt users for incorrect input content. Many people use toast directly.

 

Toast. makeText (LoginActivity. this, "please contact the Residential Property Management", Toast. LENGTH_SHORT). show ();

However, when using this method, you will find that I keep clicking the login button when I have not entered the user name, and the program will always prompt the words "Please enter the user name". When you do not click, the program will also prompt that the prompt will not be stopped until the prompt is consistent with the number of clicks, which is extremely bad for the user experience, so it provides a toast class,

 

 

 

public class CustomToast {private static Toast mToast;private static Handler mhandler = new Handler();private static Runnable r = new Runnable() {public void run() {mToast.cancel();};};public static void showToast(Context context, String text, int duration) {mhandler.removeCallbacks(r);if (null != mToast) {mToast.setText(text);} else {mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);}mhandler.postDelayed(r, 5000);mToast.show();}public static void showToast(Context context, int strId, int duration) {showToast(context, context.getString(strId), duration);}}

In this way, the toast message can be resolved.

 

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.