Toast的一種新的實現:frenchtoast

jopen 9年前發布 | 12K 次閱讀 Android開發 移動開發 frenchtoast

Toast的一種新的實現,摒棄了原生Toas的諸多缺點,例如不能很好的控制它的出現以及持續時間、打亂上下文以及API設計的不合理,容易出錯等缺點,它通過為每一個Toast創建一個新的Window來實現真正的Toast。

Android Toasts are amazing, but they have a few major drawbacks:

  • You cannot control when they show up as well as their duration. Other apps can enqueue toasts that will delay yours from showing up.
  • They break context: they remain on screen when the user switches to other activities.
  • The API is error prone:Toast.makeText(context, "Important Toast", LENGTH_LONG); // Don't forget show()!

FrenchToast gives you absolute control over your app Toasts. It does so by duplicating the internals of Android Toasts and giving you access.

Unlike other Toast-like libraries, FrenchToast doesn't add a view to the root of your activity. Instead, it creates a new Window for each Toast, exactly like the real Android Toasts.

demo.gif

Getting Started

In yourbuild.gradle:

 dependencies {
   compile 'info.piwai.frenchtoast:frenchtoast:1.0' }

You need to setupFrenchToastin yourApplicationclass:

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    FrenchToast.install(this);
  }
}

You are ready to Toast!

FrenchToast.with(context).showText("I love Baguettes!");

AFrenchToast:

  • hides when the Activity is paused,
  • shows again when it's resumed,
  • has a default duration ofToast.LENGTH_LONG,
  • survives configuration changes,
  • is queued, so that only one Toast shows at once.

You can customize the default duration:

FrenchToast.with(context).shortLength().showText(R.string.short_bread);

FrenchToast.with(context).longLength().showText(R.string.long_bread);

FrenchToast.with(context).length(3, SECONDS).showText(R.string.bespoke_bread);

項目主頁:http://www.baiduhome.net/lib/view/home/1442288160321

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!