Toast的一种新的实现:frenchtoast

jopen 9年前

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.open-open.com/lib/view/home/1442288160321