這可能是一個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