当前位置: 代码迷 >> 综合 >> Android Toast 的用法
  详细解决方案

Android Toast 的用法

热度:50   发布时间:2024-01-04 02:22:19.0

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

  相关解决方案