您的位置:首页 > 移动开发 > Android开发

Android开发工具类之Toast

2017-03-30 04:24 302 查看
Toast遇到一次写一次好麻烦,所以弄成一个工具类,调用方便点。

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wsp.thinkpad.aa.R;

/**
* Created by ${吴心良}
* on 2017/2/17.
* description:
*/

public class ToastUtils {
private static Toast toast;

public static void showToast(final Context context, final String message, final int duration) {
toast = Toast.makeText(context.getApplicationContext(), message, duration);
toast.show();
}
public static void showIconToast(Context context, String textId, int iconId, int colorId) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast, null);
((TextView) layout).setText(textId);
((TextView) layout).setTextColor(context.getResources().getColor(colorId));
((TextView) layout).setCompoundDrawablesWithIntrinsicBounds(iconId, 0, 0, 0);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}

public static void showLongToast(final Context context, final String message) {
showToast(context, message, Toast.LENGTH_LONG);
}

public static void showShortToast(final Context context, final String message) {
showToast(context, message, Toast.LENGTH_SHORT);
}
}


下面是xml代码 想怎么设置就怎么设置

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="12dp"
android:padding="12dp"
android:gravity="center"
android:background="#999999">

</TextView>


参考了下这个:https://my.oschina.net/banxi/blog/56007
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  工具类