下面列出了android.widget.Toast#setDuration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showToast(Activity activity, String message, boolean vibrateLong) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
activity.findViewById(R.id.custom_toast_container));
TextView text = layout.findViewById(R.id.text);
text.setText(message);
Vibrator v = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrateLong) {
v.vibrate(100);
v.vibrate(200);
} else {
v.vibrate(100);
}
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, convertDimensionToPx(this.getContext(), R.dimen.stdpadding));
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public static @CheckResult
Toast snackBar(Context context, CharSequence message_to_show, boolean duration) {
@SuppressLint("InflateParams")
View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_snackbar, null);
AppCompatTextView message = view.findViewById(R.id.message);
message.setText(message_to_show);
Toast toast = new Toast(context);
toast.setView(view);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
if (duration) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
return toast;
}
/**
* Call me from the UI thread, please.
*
* Meant for use by broadcast receivers receiving ACTION_BADGE_EARNED.
* */
public void toastBadge(Badge badge) {
BadgeCategory category = badge.getCategory();
// Dao<BadgeCategory, Integer> dao = dataService.getHelper().getDao(BadgeCategory.class);
// dao.refresh(category);
Toast toast = new Toast(dataService);
View content = LayoutInflater.from(dataService).inflate(R.layout.badge, null, false);
ImageView iconView = (ImageView) content.findViewById(R.id.badge_image);
TextView pointsView = (TextView) content.findViewById(R.id.badge_points);
TextView titleView = (TextView) content.findViewById(R.id.badge_title);
TextView descView = (TextView) content.findViewById(R.id.badge_description);
iconView.setImageResource(category.getIconResourceId());
int points = badge.getPoints();
if (points > 0) {
pointsView.setText(points + "");
} else {
pointsView.setVisibility(View.GONE);
}
titleView.setText(badge.getDescription());
descView.setText(badge.getSafe_extended_description());
toast.setView(content);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 200);
toast.show();
}
public static void showToast(@NonNull Context context, @NonNull String msg, int duration, int icon) {
if (!msg.isEmpty()) {
CustomToastBinding customToastBinding = CustomToastBinding.inflate(LayoutInflater.from(context));
customToastBinding.setMessage(Html.fromHtml(msg).toString());
Toast toast = new Toast(context);
toast.setGravity(Gravity.BOTTOM, 0, 150);
toast.setView(customToastBinding.getRoot());
toast.setDuration(duration);
toast.show();
}
}
/** 显示自定义Toast提示(来自String) **/
protected void showCustomToast(String text) {
View toastRoot = LayoutInflater.from(BaseActivity.this)
.inflate(R.layout.common_toast, null);
((TextView) toastRoot.findViewById(R.id.toast_text)).setText(text);
Toast toast = new Toast(BaseActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastRoot);
toast.show();
}
public static Toast makeText(Context context, CharSequence text) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.de_ui_toast, null);
result.setView(v);
TextView tv = (TextView) v.findViewById(android.R.id.message);
tv.setText(text);
result.setGravity(Gravity.CENTER, 0, 0);
result.setDuration(Toast.LENGTH_SHORT);
return result;
}
public void showToast() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, null);
Toast toastLocal = new Toast(getApplicationContext());
toastLocal.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toastLocal.setDuration(Toast.LENGTH_LONG);
toastLocal.setView(layout);
toastLocal.show();
}
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();
}
}
public static void show(Context context, CharSequence text, int duration) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View view = inflater.inflate(R.layout.tt_speeker_layout, null);
TextView title = (TextView) view.findViewById(R.id.top_tip);
title.setText(text);
Toast toast = new Toast(context.getApplicationContext());
toast.setGravity(
Gravity.FILL_HORIZONTAL | Gravity.TOP,
0,
(int) context.getResources().getDimension(
R.dimen.top_bar_default_height));
toast.setDuration(duration);
toast.setView(view);
toast.show();
}
public Toast build() {
if (!DialogUtils.checkNull(mToast)) {
mToast.get().cancel();
}
Toast toast = new Toast(context);
if (isFill) {
toast.setGravity(gravity | Gravity.FILL_HORIZONTAL, 0, yOffset);
} else {
toast.setGravity(gravity, 0, yOffset);
}
toast.setDuration(duration);
toast.setMargin(0, 0);
if(layout==0){
CardView rootView = (CardView) LayoutInflater.from(context).inflate(R.layout.view_toast_custom, null);
TextView textView = rootView.findViewById(R.id.toastTextView);
TextView descTv = rootView.findViewById(R.id.desc);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//rootView.setElevation(elevation);
rootView.setCardElevation(elevation);
}
rootView.setRadius(radius);
rootView.setCardBackgroundColor(backgroundColor);
//rootView.setBackgroundColor(backgroundColor);
textView.setTextColor(textColor);
textView.setText(title);
if(TextUtils.isEmpty(desc)){
descTv.setVisibility(View.GONE);
}else{
descTv.setText(desc);
descTv.setVisibility(View.VISIBLE);
}
toast.setView(rootView);
}else {
View view = LayoutInflater.from(context).inflate(layout, null);
toast.setView(view);
}
mToast = new SoftReference<>(toast);
return toast;
}
public static void toastWithCat(Context context, CharSequence text, boolean isHappy) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.de_ui_toast, null);
result.setView(v);
ImageView iv = (ImageView) v.findViewById(android.R.id.icon);
TextView tv = (TextView) v.findViewById(android.R.id.message);
tv.setText(text);
result.setGravity(Gravity.CENTER, 0, 0);
result.setDuration(Toast.LENGTH_SHORT);
result.show();
}
public ToastDelegate(View view, ToastInfo toastInfo) {
final Toast toast = new Toast(ContextProvider.get());
toast.setView(view);
toast.setGravity(toastInfo.gravity(), 0, toastInfo.offsetY());
toast.setDuration(Toast.LENGTH_LONG);
mToast = toast;
mDuration = toastInfo.duration() <= 0 ? 2000 : toastInfo.duration();
mToastInfo = toastInfo;
}
/**
* Show the toast for the current application.
*
* @param message the message content
* @param duration the duration
* @param icon the icon resource
* @param gravity the gravity of the toast
*/
public static void showToast(String message, int duration, int icon, int gravity) {
if (message == null || message.equalsIgnoreCase("")) return;
long time = System.currentTimeMillis();
if (!message.equalsIgnoreCase(lastToast) || Math.abs(time - lastToastTime) > 2000) {
View view = LayoutInflater.from(context()).inflate(
R.layout.view_toast, null);
((TextView) view.findViewById(R.id.tv_text)).setText(message);
if (icon != 0) {
ImageView mIcon = (ImageView) view.findViewById(R.id.iv_icon);
mIcon.setImageResource(icon);
mIcon.setVisibility(View.VISIBLE);
}
Toast toast = new Toast(context());
toast.setView(view);
toast.setGravity(gravity, 0, TDevice.getActionBarHeight(context()));
toast.setDuration(duration);
toast.show();
lastToast = message;
lastToastTime = System.currentTimeMillis();
}
}
public CT(Builder builder) {
LayoutInflater inflater = (LayoutInflater) builder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout, null);
TextView tv = layout.findViewById(R.id.cltv);
tv.setText(builder.text);
tv.setTextColor(builder.textCol);
ImageView iv = layout.findViewById(R.id.cliv);
iv.setImageResource(builder.imageRes);
GradientDrawable shape = new GradientDrawable();
shape.setShape(builder.shape);
shape.setCornerRadii(new float[]{
builder.radiusTopLeft,
builder.radiusTopLeft,
builder.radiusTopRight,
builder.radiusTopRight,
builder.radiusBottomRight,
builder.radiusBottomRight,
builder.radiusBottomLeft,
builder.radiusBottomLeft
});
shape.setColor(builder.backCol);
shape.setStroke(builder.borderWidth, builder.borderCol);
layout.setBackgroundDrawable(shape);
Toast toast = new Toast(builder.context);
toast.setView(layout);
toast.setDuration(builder.toastDuration);
toast.setGravity(builder.toastGravity,0,100);
toast.show();
}
public static void showCentorTextToast(Context context, String text) {
if (mToast != null) {
mToast.cancel();
}
View layout = LayoutInflater.from(context).inflate(R.layout.toast_center_text, (ViewGroup) ((Activity) context).findViewById(R.id.toast_layout_root));
((TextView) layout.findViewById(R.id.text)).setText(text);
Toast toast = new Toast(context);
toast.setGravity(16, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
private void startVoiceTimer() {
Log.d(TAG, "startVoiceTimer: ");
final int countdown = getArguments().getInt(EXTRA_TIMER_DURATION_SECONDS);
mTimerCountdownToast = new Toast(getActivity().getApplicationContext());
mTimerCountdownToast.setGravity(Gravity.CENTER, 0, 0);
mTimerCountdownToast.setDuration(Toast.LENGTH_SHORT);
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_timer,
(ViewGroup) getActivity().findViewById(R.id.toast_layout_root));
mTimerCountdownToast.setView(layout);
final TextView label = (TextView) layout.findViewById(R.id.countdown_text);
Timer timer = new Timer("camera_timer");
timer.scheduleAtFixedRate(new TimerTask() {
private int mCountdown = countdown;
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCountdown < 0) {
Log.e(TAG, "Take photo: " + mCountdown);
mTimerCountdownToast.cancel();
takePicture();
} else {
Log.e(TAG, "Execute timer: " + mCountdown);
label.setText(String.format("Photo in %d", mCountdown));
mTimerCountdownToast.show();
}
}
});
mCountdown--;
if (mCountdown < 0) {
cancel();
}
}
}, 1000, 1000);
}
/**
* Show custom Toast Message.
*
* @param activity Activity for show toast.
* @param toastType Type of toast.
* @param message String to show.
*/
public static void showToast(Activity activity, int toastType, String message, ToastLength toastLength) {
if (activity == null) {
Timber.e(new RuntimeException(), "Called showToast with null activity.");
return;
}
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom, (ViewGroup) activity.findViewById(R.id.toast_layout_root));
TextView text = layout.findViewById(R.id.toast_text);
ImageView iv = layout.findViewById(R.id.toast_image);
String str = "";
int icon = 0;
Toast toast = new Toast(activity);
switch (toastLength) {
case SHORT:
toast.setDuration(Toast.LENGTH_SHORT);
break;
case LONG:
toast.setDuration(Toast.LENGTH_LONG);
break;
default:
Timber.e("Not implemented");
}
switch (toastType) {
case TOAST_TYPE_MESSAGE:
str = message;
break;
case TOAST_TYPE_INTERNAL_ERROR:
str = activity.getString(R.string.Internal_error);
break;
case TOAST_TYPE_NO_NETWORK:
str = activity.getString(R.string.No_network_connection);
break;
case TOAST_TYPE_NO_SIZE_SELECTED:
str = activity.getString(R.string.Please_select_a_size);
break;
}
text.setText(str);
if (icon != 0) {
iv.setImageResource(icon);
iv.setVisibility(View.VISIBLE);
} else {
iv.setVisibility(View.GONE);
}
toast.setView(layout);
toast.show();
}
public void showMessage(View view) {
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
}
public void onClick(@SuppressWarnings("unused") View v) {
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(getLayoutInflater().inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root)));
toast.show();
}
/**
* 根据字符串获取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;
}