浪了好几天都没写博客,挺忙碌的,昨天被推荐一个第三方的 Toaaty感觉不错,所以自己测试玩了一下,非常简单好用~


首先添加依赖:

第一步:在android的大括号中添加:

allprojects {    repositories {        jcenter()        //第一步        maven { url "https://jitpack.io" }    }}
第二步:在dependencies的大括号中添加依赖:

compile 'com.github.GrenderG:Toasty:1.1.3'

PS:在Android Studio的buildgradle中buildscript和all projects的作用和区别是什么?

buildscript中的声明是gradle脚本自身需要使用的资源。可以声明的资源包括依赖项、第三方插件、maven仓库地址等。

而在build.gradle文件中直接声明的依赖项、仓库地址等信息是项目自身需要的资源。


然后就可以使用:

MainActivity的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    @Bind(R.id.button2)    Button button2;    @Bind(R.id.button3)    Button button3;    @Bind(R.id.button4)    Button button4;    @Bind(R.id.button5)    Button button5;    @Bind(R.id.button6)    Button button6;    @Bind(R.id.button7)    Button button7;    @Bind(R.id.button8)    Button button8;    @Bind(R.id.button9)    Button button9;    @Bind(R.id.button10)    Button button10;    Context context = MainActivity.this;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        button2.setOnClickListener(this);        button3.setOnClickListener(this);        button4.setOnClickListener(this);        button5.setOnClickListener(this);        button6.setOnClickListener(this);        button7.setOnClickListener(this);        button8.setOnClickListener(this);        button9.setOnClickListener(this);        button10.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            //显示成功            case R.id.button2:                Toasty.success(context, "成功", Toast.LENGTH_SHORT).show();                break;            //显示错误            case R.id.button3:                Toasty.error(context, "错误", Toast.LENGTH_SHORT).show();                break;            //显示信息            case R.id.button4:                Toasty.info(context, "信息", Toast.LENGTH_SHORT).show();                break;            //显示警告            case R.id.button5:                Toasty.warning(context, "警告", Toast.LENGTH_SHORT).show();                break;            //显示正常            case R.id.button6:                Toasty.normal(context, "通用", Toast.LENGTH_SHORT).show();                break;            //显示带自定义图标            case R.id.button7:                Toasty.normal(context, "带图标~", R.mipmap.ic_launcher).show();                break;            //显示自定义Toast            case R.id.button8:                Toasty.custom(this, "自定义", R.mipmap.ic_launcher, Color.BLACK, Color.RED, Toast.LENGTH_SHORT, true, true).show();                break;            //快捷1            case R.id.button9:                ShowToast("a消息");                break;            //快捷2            case R.id.button10:                ShowToast("b內容");                break;        }    }    /**     * @param msg     */    private void ShowToast(String msg) {        Toast toast = null;        if (toast == null) {            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);        } else {            toast.setText(msg);        }        toast.show();    }}

Xml的代码(9个按钮而已...):

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">            android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示成功Tosat" />            android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示错误Toast" />            android:id="@+id/button4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示信息Toast" />            android:id="@+id/button5"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示警告Toast" />            android:id="@+id/button6"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示正常的Toast" />            android:id="@+id/button7"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示带图标的Toast" />            android:id="@+id/button8"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示自定义Toast" />            android:id="@+id/button9"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="快捷1Toast" />            android:id="@+id/button10"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="快捷2Toast" />

其中通用带图标的测试时显示的时候是没带图标(懵逼脸),快捷的两个按钮是我自己测试Toast的重复显示问题:

 在使用Toast作为提示信息时,Toast会显示在屏幕下方,一般用来提示用户的误操作。当用户在某些情况下,用户连续误操作多次时,会导致出现很多个Toast,依次显示,会在页面上停留很长时间,这个会严重影响软件的用户亲和性。


以上描述肯定是很多人遇到的又想说的,这个方法我以前用到挺好的,但是现在这个经过我测试....没出现效果(又一·懵逼脸)


Toasty的几张截图(感觉挺好看的又简单):


更多相关文章

  1. Android(安卓)studio 使用ndk开发JNI
  2. View、Window、WindowManager-vsync信号
  3. Android(安卓)ImageView.ScaleType设置图解
  4. android 在你的UI中显示Bitmap - 开发文档翻译
  5. 【转】TextView实现文字滚动需要以下几个要点:
  6. Android消除Toast延迟显示
  7. android性能测试调试工具之dumpsys
  8. Android(安卓)StrictMode严格模式
  9. android Bitmap的截取和缩放--转

随机推荐

  1. android studio升级android.tools:gradle
  2. Android——自定义音量调节控件
  3. Android计算文件的MD5和SHA1
  4. Android------Button 添加声音效果(两种方
  5. Android(安卓)UI 之 SeekBar
  6. android堆栈打印
  7. Android(安卓)的 BroadcastReceiver
  8. 【转载】自定义圆形进度条ProgressBar的
  9. APPIUM Android(安卓)定位方式
  10. mac配置android和maven环境变量