我只需要覆盖的吐司类节目() [英] I just need to override show() for the Toast class

查看:102
本文介绍了我只需要覆盖的吐司类节目()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要重写显示()方法的吐司类。我创建延伸吐司类的类,但后来我创建了一个敬酒的消息我得到一个异常的的setView(查看视图)并没有被调用。但我不希望创建一个自定义的查看的方法,但使用默认之一。

I just need to override the show() method for the Toast class. I created a class which extends Toast class, but then I create a toast message I get an exception the setView(View view) hasn't been called. But I don't want to create a custom View for the method but use the default one.

那么,如何是可以覆盖的唯一显示()方法,无需创建定制的查看的setView(查看视图)方法?

So, how is that possible to override the only show() method without creation of custom View for the setView(View view) method?

下面是我做的:

 MyOwnToast m = new MyOwnToast(getApplicationContext());
        m.makeText(getApplicationContext(), "Bla bla bla", Toast.LENGTH_LONG);
        m.show();

和我的自定义敬酒是在这里:

And my custom toast is here:

public class MyOwnToast extends Toast {
    public MyOwnToast(Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        Log.i("Dev", "
}

此外,它是在开发网站

请注意:不要使用公共构造的吐司,除非你是
  将与的setView(查看)定义布局。如果你没有一个
  自定义布局使用,您必须使用makeText(背景下,INT,INT),以
  创建吐司。

Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View). If you do not have a custom layout to use, you must use makeText(Context, int, int) to create the Toast.

我如何可以覆盖显示()方法呢?

How I can override the show() method then?

推荐答案

你叫 super.show()您的覆盖范围内?这可能是因为 Toast.show()呼吁这一点,你的覆盖现正就那么呼叫不会发生。

Did you call super.show() within your override? It's likely that Toast.show() is calling this, and your override is now making it so that the call doesn't happen.

什么你可能需要做的是这样的:

What you'll probably need to do is something like this:

public class MyOwnToast extends Toast {
    public MyOwnToast(Toast toast) {
        //Code to initialize your toast from the argument toast

        //Probably something like this:
        this.setView(toast.getView());
        this.setDuration(toast.getDuration());
        //etc. for other get/set pairs in Toast
    }

    public static MyMakeText(Context context, CharSequence text, int duration) {
        return new MyOwnToast(Toast.makeText(context, text, duration));
    }

    public void show() {
        //Your show override code, including super.show()
    }
}

然后,当你想使用它,这样做:

Then, when you want to use it, do:

MyOwnToast.MyMakeText(activity, "Some text", Toast.LENGTH_LONG).show();

填写本表格时,请参阅敬酒文档作为参考:的http:// developer.android.com/reference/android/widget/Toast.html

这篇关于我只需要覆盖的吐司类节目()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆