---
title: "【Toast】几种展现方法"
author: "程序员程飞"
date: "2011-02-13T08:59:01Z"
tags: []
categories: ["编程语言"]
slug: "toast"
draft: false
lastmod: "2011-02-13T08:59:01Z"
id: 905127
url: "https://chengfei.iteye.com/blog/905127"
---

让我们先来认识一下她的方法：Toast.makeText(context, text, duration).show()，这样就形成了最简单的一个提示框！

参数说明：

context ：应用程序运行的环境；

text：想要提示的文本信息；

duration：（持续）表示要提示的时间长度；（不可超过5秒）。一般使用（Toast.LENGTH\_SHORT短时间、Toast.LENGTH\_LONG长时间）

 

The first:  
![](http://dl.iteye.com/upload/attachment/415419/c1d137e8-f54e-3793-801b-696fdbb1b99f.jpg)

代码如下：

```
Toast.makeText(ShowWeather.this, "城市名不能为空！", Toast.LENGTH_SHORT).show();
```

 

 

The second:  
![](http://dl.iteye.com/upload/attachment/415422/071c7ac7-63bb-3962-a5da-82ad8e10dc5f.jpg)

```
Toast ts = null;
ts = Toast.makeText(ShowWeather.this, "城市名不能为空！", Toast.LENGTH_SHORT);
ts.setGravity(Gravity.CENTER, 0, 0);
ts.show();
```