这可能是一个noob问题,但我想知道为什么我们必须使用静态方法(makeText)来创建Toast而不是构造函数。为什么Toast.makeText和不是新的吐司
我们为什么要使用这样的:
makeText(Context context, CharSequence text, int duration)
,而不是这样的:
new Toast(Context context, CharSequence text, int duration)
这是makeText方法:
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
我们为什么不有如下:
public Toast (Context context, CharSequence text, int duration) {
this(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
this.mNextView = v;
this.mDuration = duration;
}
我搜索了网页和源代码的任何原因,但我没有找到。
如果你有一个想法,请不要犹豫。
也许是因为使用静态方法更容易,我们不必创建Toast对象? :) – Slickelito 2012-07-30 08:59:39