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;
}
}
} 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;
}
}
} |