自定义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");
            }
        });
  }
}

 

效果图如下:

                                            

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现自定义Toast的显示时间: 1. 自定义一个Toast的布局文件,例如:toast_layout.xml。 2. 在代码中加载该布局文件,并设置Toast的显示时间。 ``` java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, null); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); // 设置显示时间 final int DELAY_TIME = 2000; new Handler().postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, DELAY_TIME); toast.show(); ``` 在上面的代码中,通过Toast的setView方法设置了自定义的布局文件,然后通过Handler定时取消Toast的显示。 注意:这里设置的显示时间需要根据实际情况进行调整,DELAY_TIME为毫秒数。 3. 如果需要多次显示Toast,可以将以上代码封装成一个方法,然后在需要显示Toast的地方调用该方法即可。 ``` java public static void showCustomToast(Context context, String msg) { LayoutInflater inflater = LayoutInflater.from(context); View layout = inflater.inflate(R.layout.toast_layout, null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText(msg); Toast toast = new Toast(context); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); final int DELAY_TIME = 2000; new Handler().postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, DELAY_TIME); toast.show(); } ``` 在需要显示Toast的地方调用: ``` java showCustomToast(getApplicationContext(), "自定义Toast显示时间"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值