java toast_显示Java类的Toast通知

不应该在构造函数中完成工作,这会使您单独的类无用.

public class SendError implements Runnable

{

private final Context context;

private final String url;

public SendError(Context context, String string) {

this.context = context;

this.url = string;

}

public void makeToast(String msg, String errors, String form, String database, String SQL) {

Handler handler = new Handler();

handler.post(new Runnable() {

public void run() {

Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);

toast.show();

}

});

}

}

您的上下文需要是相关的上下文,通过使用Toast,上下文通常是一种活动,可以采取以下形式:

>这(在活动中)

> ActivityName.this(在Activity的内部类中)

> getActivity(在Activity的一个片段中)

例如:

new SendError(YourActivity.this, "something").makeToast("Hello", "errors", "form", "database", "sql");

好的,我可以为您提供一些Android Java Toast自定义动画的代码,以下是示例代码: 1. 在res目录下新建anim文件夹(如果没有的话),并在该文件夹下新建一个xml文件,例如:toast_slide_in.xml,其内容如下: ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromXDelta="100%" android:toXDelta="0%" /> </set> ``` 2. 然后在res/drawable目录下创建背景文件,例如:toast_custom_bg.xml,其内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="50dp" /> <solid android:color="#FF4081" /> <padding android:bottom="10dp" android:left="12dp" android:right="12dp" android:top="10dp" /> </shape> ``` 3. 然后在Java代码创建一个自定义的Toast,代码如下: ``` public class CustomToast { public static void show(Context context, String message) { LayoutInflater inflater = LayoutInflater.from(context); View layout = inflater.inflate(R.layout.toast_layout, null, false); TextView text = layout.findViewById(R.id.toast_text); ImageView icon = layout.findViewById(R.id.toast_icon); //设置文本和图标 text.setText(message); icon.setImageResource(R.drawable.ic_launcher); Toast toast = new Toast(context); toast.setGravity(Gravity.BOTTOM, 0, 100); toast.setView(layout); toast.setDuration(Toast.LENGTH_LONG); //设置动画 Animation slideIn = AnimationUtils.loadAnimation(context, R.anim.toast_slide_in); toast.getView().startAnimation(slideIn); toast.show(); } } ``` 4. 最后,调用CustomToast.show()方法即可展示自定义的Toast,如下所示: ``` CustomToast.show(this, "这是一条自定义的Toast"); ``` 以上是示例代码,您可以根据需要修改和调整。希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值