Android-封装Toast

来源:互联网 发布:mac终端颜色配置 编辑:程序博客网 时间:2024/05/29 18:53
public class CustomToast {
/**
* Method to display toast message.

* @param activity
*            the activity context
* @param stringResId
*            the string resource id
*/
public static void showToast(final Activity activity, final int stringResId) {
try {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast toast = Toast.makeText(activity, stringResId,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} catch (Exception e) {
VCLog.error(
Category.CAT_GUI,
"CustomToast: showToast: run: Exception->"
+ e.getMessage());
e.printStackTrace();
}
}
});
} catch (Exception e) {
VCLog.error(Category.CAT_GUI, "CustomToast: showToast: Exception->"
+ e.getMessage());
e.printStackTrace();
}
}


/**
* Method to display toast message.

* @param activity
*            the activity context
* @param message
*            the message string
*/
public static void showToast(final Activity activity, final String message) {
try {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(activity, message, Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
VCLog.error(Category.CAT_GUI,
"CustomToast: showToast (message): run: Exception->"
+ e.getMessage());
e.printStackTrace();
}
}
});
} catch (Exception e) {
VCLog.error(
Category.CAT_GUI,
"CustomToast: showToast (message): Exception->"
+ e.getMessage());
e.printStackTrace();
}
}
}
原创粉丝点击