展会信息港展会大全

Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装
来源:互联网   发布日期:2015-10-03 11:19:32   浏览:4720次  

导读:Android Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能import android.content.Context; import android.os.H...

Android Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能

import android.content.Context;

import android.os.Handler;

import android.os.Looper;

import android.widget.Toast;

/**

* Toast工具类

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-21 下午9:21:01

*/

public class ToastUtils {

private static Handler handler = new Handler(Looper.getMainLooper());

private static Toast toast = null;

private static Object synObj = new Object();

/**

* Toast发送消息,默认Toast.LENGTH_SHORT

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:13:10

* @param act

* @param msg

*/

public static void showMessage(final Context act, final String msg) {

showMessage(act, msg, Toast.LENGTH_SHORT);

}

/**

* Toast发送消息,默认Toast.LENGTH_LONG

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:13:10

* @param act

* @param msg

*/

public static void showMessageLong(final Context act, final String msg) {

showMessage(act, msg, Toast.LENGTH_LONG);

}

/**

* Toast发送消息,默认Toast.LENGTH_SHORT

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:13:35

* @param act

* @param msg

*/

public static void showMessage(final Context act, final int msg) {

showMessage(act, msg, Toast.LENGTH_SHORT);

}

/**

* Toast发送消息,默认Toast.LENGTH_LONG

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:13:35

* @param act

* @param msg

*/

public static void showMessageLong(final Context act, final int msg) {

showMessage(act, msg, Toast.LENGTH_LONG);

}

/**

* Toast发送消息

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:14:09

* @param act

* @param msg

* @param len

*/

public static void showMessage(final Context act, final int msg,

final int len) {

new Thread(new Runnable() {

public void run() {

handler.post(new Runnable() {

@Override

public void run() {

synchronized (synObj) {

if (toast != null) {

toast.cancel();

toast.setText(msg);

toast.setDuration(len);

} else {

toast = Toast.makeText(act, msg, len);

}

toast.show();

}

}

});

}

}).start();

}

/**

* Toast发送消息

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:14:27

* @param act

* @param msg

* @param len

*/

public static void showMessage(final Context act, final String msg,

final int len) {

new Thread(new Runnable() {

public void run() {

handler.post(new Runnable() {

@Override

public void run() {

synchronized (synObj) {

if (toast != null) {

toast.cancel();

toast.setText(msg);

toast.setDuration(len);

} else {

toast = Toast.makeText(act, msg, len);

}

toast.show();

}

}

});

}

}).start();

}

/**

* 关闭当前Toast

* @author WikerYongEmail:<a href="#">yw_312@foxmail.com</a>

* @version 2012-5-22 上午11:14:45

*/

public static void cancelCurrentToast() {

if (toast != null) {

toast.cancel();

}

}

}

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港