Android Toast 的用法

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轴偏移量为从顶头向下的偏移量。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章