欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年11月14日 08:55:01   作者:inerdstack  
最近在開發(fā)中我們經(jīng)常會在適配5.0以后的機型遇到各種各樣的問題,其中有一個不大不小的問題就是:Toast不顯示問題,這篇文章就給大家總結(jié)了Android 5.0以上Toast不顯示的原因與解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。

原因分析

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

Toast.show()

效果圖

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

問題解決

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

Toast背景shape定義

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


Toast

因此我們使用的是shape的corners和solid結(jié)點:

<?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代碼邏輯主要模仿系統(tǒng)Toast的makeText() 、show()兩個方法,此外還需要reset()方法,實現(xiàn)Toast顯示過程中Activity切換時context也隨之切換,關(guān)鍵代碼如下:

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) {
  // 若已經(jīng)顯示,則不再次顯示
  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) {
     // 消失動畫后更改狀態(tài)為 未顯示
     isShow = false;
    }
    @Override
    public void onAnimationEnd(Animation animation) {
     // 隱藏布局,不使用remove方法為防止多次創(chuàng)建多個布局
     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);
}

方法調(diào)用

自定義Toast的使用與系統(tǒng)Toast類似,調(diào)用方法如下:

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • Android項目中引入aar包的正確方法介紹

    Android項目中引入aar包的正確方法介紹

    生成aar之后下一步就是如何引用本地的aar文件,下面這篇文章主要給大家介紹了關(guān)于Android項目中引入aar包的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • Android中ImageView.src設置圖片拉伸、填滿控件的方法

    Android中ImageView.src設置圖片拉伸、填滿控件的方法

    最近公司有個需求,要展示客戶公司的企業(yè)形象,用一張圖片放在ImageView中實現(xiàn),但是發(fā)現(xiàn)圖片并沒有填滿,而是在上下邊上留出了一點空白,下面這篇文章主要跟大家介紹了Android中ImageView.src設置圖片拉伸、填滿控件的方法,需要的朋友可以參考下。
    2017-06-06
  • 簡單實現(xiàn)Android驗證碼

    簡單實現(xiàn)Android驗證碼

    在登錄或者注冊的時候要求輸入驗證碼,這篇文章主要為大家詳細介紹了如何簡單實現(xiàn)Android驗證碼的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android 對話框sweet-alert-dialog

    Android 對話框sweet-alert-dialog

    這篇文章主要介紹了Android 對話框sweet-alert-dialog的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android開發(fā)中比較耗時的一些操作小結(jié)

    Android開發(fā)中比較耗時的一些操作小結(jié)

    這篇文章主要介紹了Android開發(fā)中比較耗時的一些操作小結(jié),本文根據(jù)實際開發(fā)經(jīng)驗總結(jié)了6條比較耗時的編程操作,請大家注意下,需要的朋友可以參考下
    2015-06-06
  • Android 密碼 顯示與隱藏功能實例

    Android 密碼 顯示與隱藏功能實例

    這篇文章主要介紹了Android 密碼 顯示與隱藏功能實例,需要的朋友可以參考下
    2017-06-06
  • android實現(xiàn)接通和掛斷電話

    android實現(xiàn)接通和掛斷電話

    這篇文章主要為大家詳細介紹了android實現(xiàn)接通和掛斷電話功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • TextView長按復制的實現(xiàn)方法(總結(jié))

    TextView長按復制的實現(xiàn)方法(總結(jié))

    下面小編就為大家?guī)硪黄猅extView長按復制的實現(xiàn)方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • android圖像繪制(四)自定義一個SurfaceView控件

    android圖像繪制(四)自定義一個SurfaceView控件

    自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下
    2013-01-01
  • Android實現(xiàn)圓角圖片

    Android實現(xiàn)圓角圖片

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圓角圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01

最新評論