Toast.makeText中参数的含义

时间:2026-02-16 15:07:30

1、Toast继承自Object,

(即java.lang.Object

        ↳android.widget.Toast)

2、官方API上写着:public static Toast makeText (Context context, int resId, int duration)

context =The context to use. Usually your Application or Activity object.

(使用上下文。通常您的应用程序或Activity对象。)

resId=The resource id of the string resource to use. Can be formatted text.(使用string资源的id去调用,或者可以使用格式化的文本。)

duration=How long to display the message. Either LENGTH_SHORT or LENGTH_LONG(显示信息时间的长短。可以是length_short或length_long)

3、或者也可以这么理解:

第一个参数:当前的上下文环境。可用getApplicationContext()或者getContext()或this

第二个参数:要显示的字符串。也可是R.string中字符串ID

第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长3.5秒)和LENGTH_SHORT(短2秒),也可以使用毫秒如1000ms(不建议)

© 2026 长短途
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com