I do not know the program ape who have not encountered this problem in the project: Click on a view pop-up Toast, the method we are using Toast.makeText (context, "prompt", Toast.LENGTH_SHORT) .show (); however, careful people found, or if you frequently click on this view, will find that even though we pulled out of the application, or will have been prompted, which is obviously a little bit awkward and a little annoying. Here we give two ways to solve this problem.
1. Packaging a small Toast:
/**
* 不循环提示的Toast
* @author way
*
*/
public class MyToast {
Context mContext;
Toast mToast;
public MyToast(Context context) {
mContext = context;
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
mToast.setGravity(17, 0, -30);//居中显示
}
public void show(int resId, int duration) {
show(mContext.getText(resId), duration);
}
public void show(CharSequence s, int duration) {
mToast.setDuration(duration);
mToast.setText(s);
mToast.show();
}
public void cancel() {
mToast.cancel();
}
}
2. The two functions function directly call: can be placed in an Activity, directly call showToast (String or int) when required; call hideToast in Activity of onPause () in () so that when the application exits, cancel annoying Toast.
/**
* Show a toast on the screen with the given message. If a toast is already
* being displayed, the message is replaced and timer is restarted.
*
* @param message
* Text to display in the toast.
*/
private Toast toast;
private void showToast(CharSequence message) {
if (null == toast) {
toast = Toast.makeText(this, message,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(message);
}
toast.show();
}
/** Hide the toast, if any. */
private void hideToast() {
if (null != toast) {
toast.cancel();
}
}
Reproduced in: https: //my.oschina.net/cjkall/blog/195779