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.