国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 5.0以上Toast不顯示的解決方法

2020-02-21 17:27:54
字體:
來源:轉載
供稿:網友

Toast在android可以顯示信息,與對話框不同是的,toast一般是沒有焦點的,toast的顯示時間是有限的,并且在一定時間后會自動消失,下文是武林技術頻道給出的Android 5.0以上Toast不顯示的解決方法,一起跟著武林技術頻道去看看吧。

原因分析

用戶使用android 5.0以上的系統在安裝APP時,將消息通知的權限關閉掉了。實際上用戶本意只是想關閉Notification,但是Toast的show方法中有調用INotificationManager這個類,而這個類在用戶關閉消息通知權限的同時被禁用了,所以我們的吐司無法顯示。

Toast.show()

效果圖

自定義Toast(上)與Toast(下)比對

問題解決

既然系統不允許我們調用Toast,那么我們就自立門戶——自己寫一個Toast出來。我們總體的思路是:在Activity的布局中添加View實現Toast的效果。

Toast背景shape定義

我們知道shape的背景是一個半透明黑色的圓角效果:


Toast

因此我們使用的是shape的corners和solid結點:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#99000000" /> <corners android:radius="8dp" /></shape>

定義布局

布局中我們主要使用TextView控件,設置相應的邊距和背景即可,布局代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mbContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="200dp" android:gravity="bottom|center" android:orientation="vertical" android:paddingLeft="50dp" android:paddingRight="50dp"> <LinearLayout android:id="@+id/toast_linear"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="@drawable/shape_toastutils_bg"  android:gravity="bottom|center"  android:orientation="vertical"  android:padding="8dp">  <TextView android:id="@+id/mbMessage"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:layout_gravity="center_vertical"   android:layout_margin="5dp"   android:layout_weight="1"   android:gravity="center"   android:shadowColor="#BB000000"   android:shadowRadius="2.75"   android:textSize="12sp"   android:textColor="#FFFFFFFF" /> </LinearLayout></LinearLayout>

java代碼邏輯

自定義Toast的java代碼邏輯主要模仿系統Toast的makeText() show()兩個方法,此外還需要reset()方法,實現Toast顯示過程中Activity切換時context也隨之切換,關鍵代碼如下:

makeText(Context context, String message, int HIDE_DELAY)方法:

public static ToastUtils makeText(Context context, String message,        int HIDE_DELAY) { if (mInstance == null) {  mInstance = new ToastUtils(context); } else {  // 考慮Activity切換時,Toast依然顯示  if (!mContext.getClass().getName().endsWith(context.getClass().getName())) {   mInstance = new ToastUtils(context);  } } if (HIDE_DELAY == LENGTH_LONG) {  mInstance.HIDE_DELAY = 2500; } else {  mInstance.HIDE_DELAY = 1500; } mTextView.setText(message); return mInstance;}

makeText(Context context, int resId, int HIDE_DELAY)方法

public static ToastUtils makeText(Context context, int resId, int HIDE_DELAY) { String mes = ""; try {  mes = context.getResources().getString(resId); } catch (Resources.NotFoundException e) {  e.printStackTrace(); } return makeText(context, mes, HIDE_DELAY);}

show()方法

public void show() { if (isShow) {  // 若已經顯示,則不再次顯示  return; } isShow = true; // 顯示動畫 mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f); // 消失動畫 mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f); mFadeOutAnimation.setDuration(ANIMATION_DURATION); mFadeOutAnimation   .setAnimationListener(new Animation.AnimationListener() {    @Override    public void onAnimationStart(Animation animation) {     // 消失動畫后更改狀態為 未顯示     isShow = false;    }    @Override    public void onAnimationEnd(Animation animation) {     // 隱藏布局,不使用remove方法為防止多次創建多個布局     mContainer.setVisibility(View.GONE);    }    @Override    public void onAnimationRepeat(Animation animation) {    }   }); mContainer.setVisibility(View.VISIBLE); mFadeInAnimation.setDuration(ANIMATION_DURATION); mContainer.startAnimation(mFadeInAnimation); mHandler.postDelayed(mHideRunnable, HIDE_DELAY);}

方法調用

自定義Toast的使用與系統Toast類似,調用方法如下:

ToastUtils.makeText(context, "消息內容",ToastUtils.LENGTH_SHORT).show();

以上就是Android 5.0以上Toast不顯示的解決方法,相信上面的文章會對你有用,建議你好好收藏,請大家隨時來武林技術頻道閱讀。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖远县| 安徽省| 云阳县| 江西省| 阿坝县| 和林格尔县| 屏东县| 囊谦县| 理塘县| 酒泉市| 巴青县| 嘉鱼县| 武定县| 邵阳市| 太康县| 郎溪县| 武强县| 淳化县| 上高县| 临高县| 井陉县| 来凤县| 施秉县| 延安市| 元朗区| 和龙市| 涟源市| 莲花县| 阳曲县| 凯里市| 扶风县| 本溪| 兴宁市| 宁海县| 当涂县| 宿迁市| 南郑县| 琼海市| 资阳市| 台中县| 双牌县|