使用Application作为Context的Toast,具有即时显示的特性。
compile 'cc.duduhuo.applicationtoast:applicationtoast:0.3'
<dependency>
<groupId>cc.duduhuo.applicationtoast</groupId>
<artifactId>applicationtoast</artifactId>
<version>0.3</version>
<type>pom</type>
</dependency>
在应用的Application类的onCreate
方法中初始化AppToast
类。如下:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AppToast.init(this);
}
}
注意:不要忘记在AndroidManifest.xml
文件中的application
节点下配置android:name
属性。
尽情使用。
AppToast.showToast("Toast 1");
AppToast.showToast(R.string.toast2);
AppToast.showToast("Toast 3", Toast.LENGTH_LONG);
AppToast.showToast(R.string.toast4, Toast.LENGTH_SHORT);
Toast toast = AppToast.getToast();
toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
toast.setText("自定义Toast");
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
方法名 | 返回值类型 | 备注 |
---|---|---|
init(Application) | void | AppToast initialization |
showToast(int) | void | Display Toast |
showToast(CharSequence) | void | Display Toast |
showToast(int, int) | void | Display Toast |
showToast(CharSequence, int) | void | Display Toast |
getToast() | Toast | Get a Toast object |
Application | Get this Application |
李颖
新浪微博:@独毒火
邮箱:liruoer2008@yeah.net
2016-12-09
Copyright 2016 LiYing
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.