简体   繁体   中英

How to display a Toast message in a TextView?

I want to display my toast message in a text view. I don't know how to copy my toast message or how to display it directly in the text view itself.

Toast already is (has) a TextView all you need is extend Toast class and make a custom toast.

Example for a custom toast class :

public class CustomToast extends Toast {

    public CustomToast(Context context) {
        super(context);
    }

    private static void processToast(Context context, Toast toast) {
        View vToast = toast.getView();

        TextView tvToast = vToast.findViewById(android.R.id.message);
        tvToast.setTypeface(yourTypeFace);
        tvToast.setGravity(Gravity.CENTER);
        toast.setGravity(Gravity.CENTER, 0, UiUtils.dpToPx(context, 28));
    }

    public static Toast toast(Context context, String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        processToast(context, toast);
        toast.show();
        return toast;
    }
}

Although it seems you are doing something wrong because normally you shouldn't need it.
Standard method seems to be something like this :

Toast.makeText(context, YOUR_TEXT_HERE, Toast.LENGTH_SHORT);
textView.setText(YOUR_TEXT_HERE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM