Android(安卓)Toast 保持一直显示。
16lz
2021-12-04
package android.widget;import android.app.ITransientNotification;import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import java.lang.reflect.Field;public class ToastUtils { private static Toast mToast = null ; public static final int MEDIA_TYPE_MUSIC = 0x00000001; public static final int MEDIA_STATUS_LOADING = 0x10000000; public static void showMediaStatusToast(Context context, int mediaType, String msg) { TextView toastMsg = null; ImageView toastImage = null; if (mToast == null) { LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View toastView = inflate.inflate(com.android.internal.R.layout.media_ready_toast, null); toastMsg = ((TextView) toastView.findViewById(com.android.internal.R.id.tv_toast_clear)); toastImage = (ImageView) toastView.findViewById(com.android.internal.R.id.iv_toast_clear); mToast = new Toast(context.getApplicationContext()); mToast.setDuration(Toast.LENGTH_SHORT); mToast.setGravity(Gravity.CENTER, 0, 0); mToast.setView(toastView); } else { toastMsg = (TextView) mToast.getView().findViewById(com.android.internal.R.id.tv_toast_clear); toastImage = (ImageView) mToast.getView().findViewById(com.android.internal.R.id.iv_toast_clear); } switch(mediaType) { case MEDIA_TYPE_MUSIC://music toastImage.setImageResource(com.android.internal.R.drawable.img_no_usb); break; case MEDIA_STATUS_LOADING://loading toastImage.setImageResource(com.android.internal.R.drawable.media_progress); toastMsg.setText(msg); showProgress(); break; } if (mediaType != MEDIA_STATUS_LOADING) { toastMsg.setText(msg); mToast.show(); } } private static void showProgress() { if (mToast != null) { try { Field toastNextView = mToast.getClass().getDeclaredField("mNextView"); toastNextView.setAccessible(true); Object toastView = toastNextView.get(mToast); toastNextView.setAccessible(false); Field mTN = mToast.getClass().getDeclaredField("mTN"); mTN.setAccessible(true); Object tn = mTN.get(mToast); mTN.setAccessible(false); Field tnView = tn.getClass().getDeclaredField("mNextView"); tnView.setAccessible(true); tnView.set(tn, toastView); tnView.setAccessible(false); ITransientNotification notification = (ITransientNotification) mTN.get(mToast); notification.show(); } catch (Exception e) { e.printStackTrace(); } } } public static void hideProgress() { if (mToast != null) { try { Field mTN = mToast.getClass().getDeclaredField("mTN"); ITransientNotification notification = (ITransientNotification) mTN.get(mToast); notification.hide(); } catch (Exception e) { e.printStackTrace(); } } }}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer