I just need to override show() for the Toast class

后端 未结 2 866
北海茫月
北海茫月 2021-01-23 22:50

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 mess

2条回答
  •  长发绾君心
    2021-01-23 23:38

    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();
    

    See the Toast docs as a reference when filling this out: http://developer.android.com/reference/android/widget/Toast.html

提交回复
热议问题