安卓开发 Toasty的使用

浪了好几天都没写博客,挺忙碌的,昨天被推荐一个第三方的 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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示成功Tosat" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示错误Toast" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示信息Toast" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示警告Toast" />

    <Button
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示正常的Toast" />

    <Button
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示带图标的Toast" />

    <Button
        android:id="@+id/button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示自定义Toast" />

    <Button
        android:id="@+id/button9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="快捷1Toast" />

    <Button
        android:id="@+id/button10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="快捷2Toast" />
</LinearLayout>

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

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


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


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

安卓开发 Toasty的使用安卓开发 Toasty的使用安卓开发 Toasty的使用安卓开发 Toasty的使用