Android Toast封装
可以将代码放在util文件夹下,方便在项目中调用
调用方法:
SnackBarUtils.showToast(this, R.string.request_app_permission);
具体代码:
import android.content.Context; import android.view.Gravity; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.evolving.stock.R; import com.trycatch.mysnackbar.Prompt; import com.trycatch.mysnackbar.TSnackbar; /** * Created by mocaris on 2017/11/15. */ public class SnackBarUtils { private static Toast toast; // 携带显示控件 public static void showShort(View parent, String msg, Prompt type) { show(parent, msg, TSnackbar.LENGTH_SHORT, type); } public static void show(View parent, String msg, int duration, Prompt type) { TSnackbar tSnackbar = TSnackbar.make(parent, msg, duration, TSnackbar.APPEAR_FROM_TOP_TO_DOWN) .setPromptThemBackground(type); TextView msgTV = (TextView) tSnackbar.getView().findViewById(R.id.snackbar_text); msgTV.setGravity(Gravity.CENTER); msgTV.setCompoundDrawables(null, null, null, null); tSnackbar.show(); } //=============================== 不带控件 ======================================== //String 类型的提示消息 public static void showToast(Context context, String msg) { if (toast == null){ toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT); } toast.setText(msg); toast.show(); } // 在values中的String索引int型 public static void showToast(Context context, int msgResid) { if (toast == null){ toast = Toast.makeText(context, msgResid, Toast.LENGTH_SHORT); } toast.setText(msgResid); toast.show(); } }