下面列出了android.widget.Toast#setText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Implementation of makeText that actually shows the message.
*/
@Implementation
@SuppressLint("ShowToast")
public static Toast makeText(Context context, CharSequence text, int duration) {
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView messageView = new TextView(context);
layout.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
messageView.setId(android.R.id.message);
layout.addView(messageView);
Toast toast = new Toast(context);
toast.setView(layout);
toast.setDuration(duration);
toast.setText(text);
return toast;
}
private static void show(Context context, String message, int duration) {
Toast toast = getToast(context);
if (toast != null) {
toast.setText(message);
toast.setDuration(duration);
toast.show();
}
}
private static void show(Context context, @StringRes int messageRes, int duration) {
Toast toast = getToast(context);
if (toast != null) {
toast.setText(messageRes);
toast.setDuration(duration);
toast.show();
}
}
@SuppressLint("ShowToast")
public static void showToast(String content) {
DialogUtils.checkMainThread();
checkContext();
if (!DialogUtils.checkNull(mToast)) {
mToast.get().cancel();
}
Toast toast = Toast.makeText(mApp, "", Toast.LENGTH_SHORT);
toast.setText(content);
toast.show();
mToast = new SoftReference<>(toast);
}
/**
* Display a Toast with information about the application.
*
* @param packageManager usually retrieved by getPackageManager()
* @param packageName usually retrieved by getPackageName()
*/
void createAboutToast(final PackageManager packageManager, final String packageName, final Toast toast, final Context mContext) {
try {
final PackageInfo pInfo = packageManager.getPackageInfo(packageName, 0);
toast.setText(mContext.getString(R.string.toast_version) + " " + pInfo.versionName
+ mContext.getString(R.string.toast_createdBy));
toast.show();
} catch (NameNotFoundException e) {
toast.setText(R.string.toast_errorVersion);
toast.show();
}
}
private final void doShowToast(Context context, String toast, int length) {
try {
Toast t = getToast(context);
t.setText(toast);
t.setDuration(length);
t.show();
} catch (Exception e) {
Toast.makeText(context, toast, length).show();
}
}
/**
* toast兼容适配
*
* @param context {@link Context}
* @param text 文本信息
* @param isLong 是否是持续时间较长的toast
* @param isCenter 是否显示在中心位置
*/
private static void toastCompat(Context context, CharSequence text, boolean isLong, boolean isCenter) {
boolean isMI = SpecialSupport.isMIUI();
//MIUI的Toast在文本前面添加应用名称很不合理,因为Toast显示时间有限,需要尽快让用户注意最重要的内容
Toast toast = Toast.makeText(context.getApplicationContext(), isMI ? null : text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
if (isMI) toast.setText(text);
if (isCenter) toast.setGravity(Gravity.CENTER, 0, 0);
fixBadTokenException(context, toast);
toast.show();
}
/**
* @param message
*/
public static void showToast(int message, Toast mToast, Context context) {
if (mToast == null) {
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
mToast.setText(context.getString(message));
mToast.show();
}
/**
* BdToastCustom constucts
* @param context context
* @param text text
* @param time time
*/
private TamicWindowManager(Context context, String text, double time){
wdm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
timer = new Timer();
// mView = LayoutInflater.from(context).inflate(R.layout.activity_loading, null);
mView = new TamcWaitingView(context);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setMargin(0, 0);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(mView);
toast.setText(text);
params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = toast.getView().getAnimation().INFINITE;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;
params.y = -30;
this.time = time;
}
private static Toast makeNormalToast(Context context, CharSequence text, int duration) {
@SuppressLint("ShowToast")
Toast toast = Toast.makeText(context, "", duration);
toast.setText(text);
return toast;
}
/**
* 根据字符串获取toast
*
* @param ctx
* 上下文
* @param msg
* 字符串
* @return Toast对象
*/
public static Toast getToast(Context ctx, String msg) {
Toast mToast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
mToast.setText(msg);
mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 20);
mToast.setDuration(Toast.LENGTH_SHORT);
return mToast;
}