Toast 是比較簡單的控件,但很實用。
Toast 的用法
1
public void showToast(String str,int duration)
{
Toast.makeText(this, str, duration).show();
}
調用方法:
showToast("Button 1 clicked!",Toast.LENGTH_SHORT);
showToast("Button 2 clicked!",Toast.LENGTH_LONG);
2
public void showToast(String str,int duration)
{
//Toast.makeText(this, str, duration).show();
Toast toast = Toast.makeText(this, str, duration);
toast.setGravity(Gravity.TOP, 0, 200);
toast.show();
}
調用方法:
showToast("Checkbox " + box3.getText(),Toast.LENGTH_SHORT);
showToast("UnCheckbox " + box3.getText(),Toast.LENGTH_SHORT);
注:
Toast的一般用法爲:Toast.makeText(this, "顯示的文字", duration).show(); duration爲0是短時間顯示,爲1是長時間顯示。
toast.setGravity(Gravity.TOP, 0, 200); //是設置toast出現的位置,第一個參數是重力,第二個參數是x軸偏移量,第三個參數是y軸偏移量。Gravity.TOP後x軸偏移量負值爲屏幕左邊,正值爲屏幕右邊。y軸偏移量爲從頂頭向下的偏移量。