如何集中创建自定义 Toast

发布于 2024-11-29 04:39:19 字数 626 浏览 2 评论 0原文

在我的应用程序中,我几乎在所有活动中都使用自定义 toast。要创建自定义吐司,我有以下方法:

private void getCustomToast(String message)
{
    LayoutInflater li   = getLayoutInflater();
    View toastlayout    = li.inflate(R.layout.toast_error, (ViewGroup)findViewById(R.id.toast_layout));
    TextView text       = (TextView) toastlayout.findViewById(R.id.toast_text);
    text.setText(message);

    Toast toast = new Toast(this);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(toastlayout);
    toast.show();
}

它工作正常,但对于每个活动,我需要重复此方法,而不是真正尊重 DRY 原则...

我如何制作一个静态类(例如),其中我有一种在当前活动中触发自定义 toast 的方法?

谢谢

in my application i use a custom toast in almost all the activity. To create the custom toast i have the following method :

private void getCustomToast(String message)
{
    LayoutInflater li   = getLayoutInflater();
    View toastlayout    = li.inflate(R.layout.toast_error, (ViewGroup)findViewById(R.id.toast_layout));
    TextView text       = (TextView) toastlayout.findViewById(R.id.toast_text);
    text.setText(message);

    Toast toast = new Toast(this);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(toastlayout);
    toast.show();
}

It works fine but for each activity I need to duplicate this method, not really respectfull of the DRY principle ...

How can i make a static class (for example) in which i have a method which gonna fire the custom toast on the current activity ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-12-06 04:39:19

您应该创建一个包含 toast 方法的自定义抽象 Activity,然后为您的应用程序的活动扩展它:

public abstract class ToastActivity extends Activity {

    protected void getCustomToast(String message)
    {
        LayoutInflater li = getLayoutInflater();
        View toastlayout  = li.inflate(
                R.layout.toast_error, 
                (ViewGroup) findViewById(R.id.toast_layout));

        TextView text = (TextView) toastlayout.findViewById(R.id.toast_text);
        text.setText(message);

        Toast toast = new Toast(this);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastlayout);
        toast.show();
    }

}

You should make a custom abstract Activity that contains the toast method, and then extend that for your application's activities:

public abstract class ToastActivity extends Activity {

    protected void getCustomToast(String message)
    {
        LayoutInflater li = getLayoutInflater();
        View toastlayout  = li.inflate(
                R.layout.toast_error, 
                (ViewGroup) findViewById(R.id.toast_layout));

        TextView text = (TextView) toastlayout.findViewById(R.id.toast_text);
        text.setText(message);

        Toast toast = new Toast(this);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastlayout);
        toast.show();
    }

}
〃安静 2024-12-06 04:39:19

我想说你可以创建一个接受 Context 对象的 [static] 类。然后使用:

Toast toast = Toast.makeText(context, text, duration).show();

所以在你的静态类中:

public static class Utility
{
    public static void toast(Context context, String msg)
         {
              Toast toast = Toast.makeText(context, msg, Toast.DURATION_LONG).show();

         }

}

或者类似的东西

I want to say you can create a [static] class which will accept a Context object. Then use:

Toast toast = Toast.makeText(context, text, duration).show();

So in your static class:

public static class Utility
{
    public static void toast(Context context, String msg)
         {
              Toast toast = Toast.makeText(context, msg, Toast.DURATION_LONG).show();

         }

}

Or something like that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文