Toast工具类

来源:互联网 发布:神经网络数据预处理 编辑:程序博客网 时间:2024/05/17 03:44


import com.eyeguard.app.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 主要功能: 自定义Toast提示框
 */
public class AppToastMgr {



//对话框时长号(毫秒)
private static int duration = 200;

//自定义toast对象
private static Toast toast;


/**
* 自定义Toast调用
* @param context 上下文
* @param message 显示文本
* @return void   
*/
public static void shortToast(final Context context, final String message) {
if (null == toast) {
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sys_show_toast, null);
TextView textView = (TextView) view.findViewById(R.id.sys_show_toast_txt);
textView.setText(message);
toast.setView(view);
toast.show();
} else {
TextView textView = (TextView) toast.getView().findViewById(R.id.sys_show_toast_txt);
textView.setText(message);
toast.show();
}
}



/**
* 自定义Toast调用
* @param context 上下文
* @param message 显示文本
* @return void   
*/
public static void longToast(final Context context, final String message) {
if (null == toast) {
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sys_show_toast, null);
TextView textView = (TextView) view.findViewById(R.id.sys_show_toast_txt);
textView.setText(message);
toast.setView(view);
toast.show();
} else {
TextView textView = (TextView) toast.getView().findViewById(R.id.sys_show_toast_txt);
textView.setText(message);
toast.show();
}
}

/**
* 取消显示Toast

* create by fuxiaosong
*/
public static void cancelToast() {
if (null != toast) {
toast.cancel();
}
}

/**
* 默认Toast调用
* @param context 上下文
* @param message 显示文本
*/
public static void Toast(final Context context, final String message) {
Toast.makeText(context, message, duration).show();
}

}

这里是用到的布局文件,一并贴出来

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:background="@drawable/sys_toast" >
    <!--提示文本 -->
    <TextView
        android:id="@+id/sys_show_toast_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" 
        android:textColor="@color/white" />
</LinearLayout>

最后附上所有工具类的下载链接:

http://download.csdn.net/detail/u014727709/9697759

转载自:http://blog.csdn.net/u014727709/article/details/53390535

欢迎start,欢迎评论,欢迎指正

1 0
原创粉丝点击