1. 程式人生 > >自定義Toast工具

自定義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">
// <color name="colorSecondPrimary">#536590</color>
    <solid android:color="@color/colorSecondPrimary" />

    <corners android:radius="@dimen/_5px_in720p" />
    <stroke
        android:width="@dimen/_2px_in720p"
        //    <color name="color_999999">#999999</color>
        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的地方彈土司;