现在的位置: 首页 > 综合 > 正文

在Android中使用Toast进行提示

2013年09月21日 ⁄ 综合 ⁄ 共 3600字 ⁄ 字号 评论关闭

Toast可能是1个相当有Android特色的东西,在现实中也经常被用到,本教程会对Toast的使用进行1个比较全面的总结,一共有4个例子,分别讲述最简单的用法,如何调整显示位置,以及如何创建自定义的Toast显示。
这篇教程,以代码为主,注释中对使用方法进行了一些讲解,基本上可以作为1个Toast用法速查。
Android中经常用到的另1种提示方法可以参见另一篇教程:《教程:在Android中使用Notification进行提示》

Activity: ToastSample.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import net.learningandroid.apitest.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
 
/**
 * Toast 使用大全.
 * @author bing
 *
 */
public class ToastSample extends Activity implements View.OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.toast_sample);
 
		findViewById(R.id.btnToastNormal).setOnClickListener(this);
		findViewById(R.id.btnToastPosition).setOnClickListener(this);
		findViewById(R.id.btnToastMargin).setOnClickListener(this);
		findViewById(R.id.btnToastCustomView).setOnClickListener(this);
	}
 
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btnToastNormal:
				//最普通的调用方式,显示时长为了short,这个值根据不同的系统会有不同,也可自行指定毫秒数
				//第1个参数是Context,一般直接指定为当前Activity实例即可
				//第2个参数是要显示的文本,此处直接使用String,建议使用在xml中预定义的string
				//第3个参数是显示时长,单位为毫秒数,此处使用了预定义的Toast.LENGTH_SHORT,
				//另有Toast.LENGTH_LONG可以使用,这2个值会根据系统而略有不同
				//别忘了最后的.show()
				Toast.makeText(this, "Toast text, normal", Toast.LENGTH_SHORT).show();
				break;
			case R.id.btnToastPosition:
				//默认的Gravity就是Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM
				//此处只对 yOffset 进行调整,让文本显示的位置更往靠下一些
				Toast t2=Toast.makeText(this, "Toast text with specific position", Toast.LENGTH_LONG);
				t2.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 10);
				t2.show();
				break;
			case R.id.btnToastMargin:
				//如果希望对显示位置进行较大幅度的调整,建议使用了setMargin方法
				//setMargin接受的参数分别是横向和纵向的百分比,这样在不同分辨率下的适应力更好。
				//此处是修改为在屏幕纵向正中间的上方显示
				Toast t3=Toast.makeText(this, "Toast text with specific margin and position", Toast.LENGTH_SHORT);
				t3.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 0);
				t3.setMargin(0f, 0.5f);
				t3.show();
				break;
			case R.id.btnToastCustomView:
				//使用自定义的View来显示Toast,必须先编写1个layout定义文件
				//事实上Toast.makeText方法也是这样调用的
				LayoutInflater inflater = getLayoutInflater();
				View layout = inflater.inflate(R.layout.toast_view_sample,
				                               (ViewGroup) findViewById(R.id.toastSampleLayout), false);
 
				TextView text = (TextView) layout.findViewById(R.id.toast_text);
				text.setText("Toast with custom view, it's a long text :" +
						" Manuka honey interferes with bacteria infecting a wound" +
						" by keeping the microbes from attaching to tissue" +
						" and even by making antibiotics more effective." +
						" Cynthia Graber reports. ");
 
				Toast t4 = new Toast(this);
				t4.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 50);
				t4.setDuration(Toast.LENGTH_LONG);
				t4.setView(layout);
				t4.show();				
				break;
			default:
				break;
		}
	}
}

第4个例子中使用的自定义View的定义文件:toast_view_sample.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:id="@+id/toastViewLayout" android:orientation="horizontal"
	android:padding="20dp" android:background="@android:drawable/toast_frame"
>
    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="10dp"
               android:src="@drawable/toast_icon"
               />
    <TextView android:id="@+id/toast_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFFFFFFF"
              />
</LinearLayout>

这里用到@android:drawable/toast_frame其实就是Android默认的Toast所使用的背景,而@drawable/toast_icon则是我随便找了1个图标来用的。
最终运行效果截图:
Toast NormalToast with Specific Position
Toast with Specific MarginToast with Custom View

© 2011,
Bing
. 版权所有。 所有转载请以链接方式进行。

抱歉!评论已关闭.