Log和Toast都是在顯示操作結果,兩者的差異在於Log不會顯示在畫面上,Toast會將結果顯示在畫面。程式撰寫也十分單純,與print類似。
使用方式不需要先初始化Toast就可以用,Toast是一個浮動式的提示訊息,當我們執行事件的時候會顯示成功或失敗的提示訊息讓我們知道目前的狀態是什麼。
LENGTH_SHORT
是預設顯示時長,可以依照需求更改成LENGTH_LONG
。.show()
才能顯示懸浮提示文字。
public static Toast makeText(Context context, CharSequence text, int duration) {
throw new RuntimeException("Stub!");
}
builder.setItems (Countrys, new DialogInterface.OnClickListener () {
@Override
public void onClick (DialogInterface dialogInterface, int i) {
Toast.makeText (MainActivity.this,"Now you selected Country is : "+Countrys[i],Toast.LENGTH_LONG).show ();
}
});
builder1.setMultiChoiceItems (Foods, selected, new DialogInterface.OnMultiChoiceClickListener () {
@Override
public void onClick (DialogInterface dialogInterface, int i, boolean b) {
//點選選項時
Toast.makeText (MainActivity.this,"Now you selected Food is : "+Foods[i],Toast.LENGTH_SHORT).show ();
}
}).setPositiveButton ("Positive", new DialogInterface.OnClickListener () {
//點選PositiveButton
@Override
public void onClick (DialogInterface dialogInterface, int i) {
Toast.makeText (MainActivity.this,"已送出",Toast.LENGTH_SHORT).show ();
}
}).setNegativeButton ("Negative", new DialogInterface.OnClickListener () {
//點選NegativeButton
@Override
public void onClick (DialogInterface dialogInterface, int i) {
Toast.makeText (MainActivity.this,"取消選取",Toast.LENGTH_SHORT).show ();
}
}).setNeutralButton ("Neutral", new DialogInterface.OnClickListener () {
//點選NeutralButton
@Override
public void onClick (DialogInterface dialogInterface, int i) {
Toast.makeText (MainActivity.this,"退出",Toast.LENGTH_SHORT).show ();
}
PositiveButton
、NegativeButton
以及NutralButton
的效果皆與上方類似,我就不貼圖片了。Log是一種在撰寫程式時蠻有用的功能,可以查看是否有執行該事件,以及可以清楚查看到結果,更有多種使用方式,Android Studio_Log的各種型態。
//Log.型態("標籤","訊息");
Log.wtf ("WTF","WTF msg"+Foods[i]+b);
Log.w ("Warn","Warn msg"+Foods[i]+b);
Log.i ("Info","Info msg"+Foods[i]+b);
Log.e ("Error","Error msg"+Foods[i]+b);
Log.d ("Debug","Debug msg"+Foods[i]+b);
//Log.println("型態","標籤","訊息");
Log.println (Log.ASSERT,"println_Assert ","Println msg"+Foods[i]+b);
Log.println (Log.VERBOSE,"println_Verbose ","Println msg"+Foods[i]+b);
Log.println (Log.DEBUG,"println_Debug ","Println msg"+Foods[i]+b);
Log.println (Log.ERROR,"println_Error ","Println msg"+Foods[i]+b);
Log.println (Log.INFO,"println_Info ","Println msg"+Foods[i]+b);
Log.println (Log.WARN,"println_Warn ","Println msg"+Foods[i]+b);
根據選擇的型態不同可以觀察到所限制的Log型態。
基本上我最常用的型態是Log.e
,因為顯示的是紅色字體所以較為容易觀察結果。
Dolphin版本有更新Logcat,詳細可以去看這一篇IT文章