Toast提示框:
1
2
3
4
5
6
7
8
9
10
|
findViewById(R.id.button1).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
//Toast.makeText(context对象, text提示内容, duration时间长短),记得加show()方法
Toast.makeText(MainActivity.
this
,
"下载完成"
, Toast.LENGTH_SHORT).show();
}
});
|
自定义Toast方法:
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
|
private
void
clickToast()
{
Toast toast =
new
Toast(
this
);
// 设置View
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
null
);
//通过布局
toast.setView(layout);
//设置显示位置和显示时间
toast.setGravity(Gravity.CENTER,
60
,
0
);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
private
void
clickToast1()
{
Toast toast =
new
Toast(
this
);
// 设置View
TextView textView =
new
TextView(
this
);
textView.setBackgroundColor(Color.BLUE);
textView.setText(
"定制toast"
);
toast.setView(textView);
//设置显示位置和显示时间
toast.setGravity(Gravity.CENTER,
60
,
0
);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1198603,如需转载请自行联系原作者