自定义Toast封装后使用

//两种Toast布局

//仅提示语

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shape_toast"
    android:minHeight="30dp"
    android:minWidth="30dp">


    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="12sp"
        tools:text=""/>


</RelativeLayout>

//图片+提示语

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:paddingTop="42dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingBottom="42dp"
        >

        <ImageView
            android:id="@+id/iv_type"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_gravity="center"
            android:src="@drawable/icon_warning_toast"
            />


        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:gravity="center"
            android:textColor="#1b1b1b"
            android:textSize="20sp"
            android:text="网络异常,请重试"
            />
    </LinearLayout>



</FrameLayout>

//自定义Toast类,可更换图片及提示语,用Builder方式创建Toast

//定制化Toast
public class CommonToast extends Toast {

    public static final int TYPE_WARNING = 1;//警告类型,显示感叹号
    public static final int TYPE_CORRECT = 2;//信息类型,显示对勾

    @IntDef({TYPE_WARNING, TYPE_CORRECT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TOAST_TYPE {
    }

    private ImageView ivType;
    private TextView tvTitle;

    public CommonToast(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_common_toast, null);
        ivType = view.findViewById(R.id.iv_type);
        tvTitle = view.findViewById(R.id.tv_title);
        setView(view);
    }

    public void setType(@TOAST_TYPE int type) {
        if (type == TYPE_WARNING) {
            ivType.setImageResource(R.drawable.icon_warning_toast);
        } else {
            ivType.setImageResource(R.drawable.icon_correct_toast);
        }
    }


    @Override
    public void setText(CharSequence s) {
        //super.setText(resId);
        tvTitle.setText(s);
    }


    public static class Builder {

        private CommonToast mCommonToast;

        public Builder(Context context) {
            mCommonToast = new CommonToast(context);
        }

        public Builder setGravity(int gravity, int xOffset, int yOffset) {
            mCommonToast.setGravity(gravity, xOffset, yOffset);
            return this;
        }

        public Builder setDuration(int duration) {
            mCommonToast.setDuration(duration);
            return this;
        }

        public Builder setText(CharSequence s) {
            mCommonToast.setText(s);
            return this;
        }

        public Builder setType(@TOAST_TYPE int type) {
            mCommonToast.setType(type);
            return this;
        }

        public CommonToast build() {
            return mCommonToast;
        }


    }

}

//通过Helper类提供

public class ToastHelper {
    private static Toast sToast;
    private static CommonToast sCommonToast;

    private static Toast getToast(Context context){
        if (sToast == null) {
            Context applicationContext = context.getApplicationContext();
            sToast = new Toast(applicationContext);
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.setView(LayoutInflater.from(applicationContext).inflate(R.layout.layout_toast,null));
        }
        return sToast;
    }

    public static void showToast(Context context,String msg){
        Toast toast = getToast(context);
        TextView textView = toast.getView().findViewById(R.id.tv_content);
        textView.setText(msg);
        toast.show();
    }

    private static CommonToast getCommonToast(Context context) {
        if (sCommonToast == null) {
            Context applicationContext = context.getApplicationContext();
            sCommonToast = new CommonToast.Builder(applicationContext)
                    .setGravity(Gravity.CENTER, 0, 0)
                    .setDuration(Toast.LENGTH_SHORT)
                    .build();
        }
        return sCommonToast;
    }

    public static void showCommonToast(Context context, CharSequence msg) {
        showCommonToast(context, msg, CommonToast.TYPE_WARNING);
    }

    public static void showCommonToast(Context context, CharSequence msg, @CommonToast.TOAST_TYPE int type) {
        CommonToast commonToast = getCommonToast(context);
        commonToast.setText(msg);
        commonToast.setType(type);
        commonToast.show();
    }


}

//在Activity中展示

public class MainActivity extends AppCompatActivity {

    Toast toast;


    AppCompatButton bt1,bt2;


    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (AppCompatButton)findViewById(R.id.bt1);
        bt2 = (AppCompatButton)findViewById(R.id.bt2);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastHelper.showToast(MainActivity.this,"show text toast");
            }
        });


        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastHelper.showCommonToast(MainActivity.this,"show common toast");
            }
        });
  }
}

效果图如下:

                                            

猜你喜欢

转载自blog.csdn.net/gz2012zb/article/details/81147479