关于Android中Toast使用小结
16lz
2022-06-11
Android中的Toast用于向用户显示一些帮助/提示。
以下是关于Toast的一些使用的总结,希望对大家的学习和解决问题提供一些帮助:
默认Toast样式:
Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();
自定义位置Toast:
Toast toast = Toast.makeText(getApplicationContext(), "自定义位置Toast",Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0);toast.show();
自定义大小Toast(自定义大小并不由代码控制,而是由布局控制,注意使用布局的padding属性)
View params=LayoutInflater.from(getApplicationContext()).inflate(R.layout.params_layout, null);TextView paramsTextView=(TextView) params.findViewById(R.id.paramsTitleToast);paramsTextView.setText("自定义大小的Toast");toast = new Toast(getApplicationContext());toast.setGravity(Gravity.CENTER, 0, 0);toast.setDuration(Toast.LENGTH_LONG);toast.setView(params);
带图片的ToastToast toast = Toast.makeText(getApplicationContext(), "带图片的Toast",Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayout toastView = (LinearLayout) toast.getView();ImageView imageCodeProject = new ImageView(getApplicationContext());imageCodeProject.setImageResource(R.drawable.ic_launcher);toastView.addView(imageCodeProject, 0);toast.show();
完全自定义ToastView layout = getLayoutInflater().inflate(R.layout.custom, null);ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);image.setImageResource(R.drawable.ic_launcher);TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);title.setText("Attention");TextView text = (TextView) layout.findViewById(R.id.tvTextToast);text.setText("完全自定义Toast");Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();
Toast重复显示的问题 问题描述:当在一个Toast还在显示状态,调用了另一个Toast的show()方法时,两个Toast会进入一个列队中,等前一个Toast显示结束以后,后一个才会显示,由于误操作等等原因的存在,导致Toast长时间存在的用户体验问题。 解决思路:每次创建Toast时先做一下判断,如果前面有Toast在显示,只需调用Toast中的setText()方法将要显示的信息替换即可。
public class CustomToast { private static Toast mToast; private static Handler mHandler = new Handler(); private static Runnable r = new Runnable() { public void run() { mToast.cancel(); } }; public static void showToast(Context mContext, String text, int duration) mHandler.removeCallbacks(r); if (mToast != null) mToast.setText(text); else mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT); mHandler.postDelayed(r, duration); mToast.show(); } public static void showToast(Context mContext, int resId, int duration) { showToast(mContext, mContext.getResources().getString(resId), duration); }}
最后提供一个项目中 Toast的统一管理类ToastUtil:
/** * Toast统一管理类 * */public class ToastUtil {private ToastUtil() {/* cannot be instantiated */throw new UnsupportedOperationException("cannot be instantiated");}public static boolean isShow = true;/** * 短时间显示Toast * * @param context * @param message */public static void showShort(Context context, CharSequence message) {if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}/** * 短时间显示Toast * * @param context * @param message */public static void showShort(Context context, int message) {if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}/** * 长时间显示Toast * * @param context * @param message */public static void showLong(Context context, CharSequence message) {if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}/** * 长时间显示Toast * * @param context * @param message */public static void showLong(Context context, int message) {if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}/** * 自定义显示Toast时间 * * @param context * @param message * @param duration */public static void show(Context context, CharSequence message, int duration) {if (isShow)Toast.makeText(context, message, duration).show();}/** * 自定义显示Toast时间 * * @param context * @param message * @param duration */public static void show(Context context, int message, int duration) {if (isShow)Toast.makeText(context, message, duration).show();}}
更多相关文章
- Android实现动态显示或隐藏密码输入框的内容
- android:sharedUserId权限问题
- tabcontent中scaleType的使用 图片Drawable和bitmap显示问题
- android改变全局字体大小
- Androd之在图片右上角显示红色圆圈里面数字提醒
- android中用Spannable在TextView中设置超链接、颜色、字体
- Android(安卓)软键盘盖住输入框的问题
- EditText 的常用属性与解释
- dex.force.jumbo和Java heap space的问题