自定义Toast工具

1.封装Toast工具

  • 创建Toast显示需要依附的ToastLayout
  • 定义TextView显示Toast内容并设置显示位置

ToastUtil .class

/**
 * @author zhou.jn on 2018/11/27 11:30.
 */

public class ToastUtil {
    private static Toast mToast;
    private static TextView textView;
    /**
     *设置可以自定义背景、边框的Toast
     */
    public static Toast makeText(CharSequence text, int duration) {
        if (MyApplication.mContext != null) {
            if (mToast == null) {
                View toastBg = LayoutInflater.from(MyApplication.mContext).inflate(R.layout.layout_toast, null);
                textView = (TextView) toastBg.findViewById(R.id.tv_toast);
                mToast = new Toast(MyApplication.mContext);
                mToast.setView(toastBg);
            }
            textView.setText(text);
            mToast.setDuration(duration);
        }
        return mToast;
    }

    /**
     * 设置默认类型的Toast
     */
    public static Toast makeText(CharSequence text) {
        if (MyApplication.mContext != null) {
            if (mToast == null) {
                Toast toast = Toast.makeText(MyApplication.mContext, text, Toast.LENGTH_SHORT);
                mToast = toast;
            }
        }
        return mToast;
    }
}

layout_toast.xml


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_toast"
    android:background="@drawable/bg_toast"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
// #536590
    <solid android:color="@color/colorSecondPrimary" />

    <corners android:radius="@dimen/_5px_in720p" />
    <stroke
        android:width="@dimen/_2px_in720p"
        //    #999999
        android:color="@color/color_999999" />
    <padding
        android:bottom="@dimen/_5px_in720p"
        android:left="@dimen/_5px_in720p"
        android:right="@dimen/_5px_in720p"
        android:top="@dimen/_5px_in720p"></padding>
</shape>

MyApplication.class

/**
 * @author zhou.jn on 2018/11/27 11:31.
 */

public class MyApplication extends Application {
    public static  Context mContext;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }
    public static Context getContext(){
        return mContext;
    }
}

2.使用
MainActivity.class


public class MainActivity extends AppCompatActivity {
    Button btn_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_click = (Button) findViewById(R.id.btn_click);
        btn_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //自定义Toast
                ToastUtil.makeText("toast showing",Toast.LENGTH_SHORT).show();
                //显示默认Toast
                ToastUtil.makeText("hello friend!").show();
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.utils.MainActivity">

    <Button
        android:id="@+id/btn_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK" />
</RelativeLayout>

3.结束
4.优点:

  • ①省去了新建Toast时要写 Toast toast = Toast.makeText(MyApplication.mContext, text, Toast.LENGTH_SHORT);这么长的字符串;
  • ②确保每次传入的Context为统一的Toast;
  • ③便于在非Activity的地方弹土司;

你可能感兴趣的:(Android,Utils)