一、toast:Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示该信息。
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失
方法:
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
cancel()
Close the view if it's showing, or don't show it if it isn't showing yet.
| ||||||||||
int |
getDuration()
Return the duration.
| ||||||||||
int |
getGravity()
Get the location at which the notification should appear on the screen.
| ||||||||||
float |
getHorizontalMargin()
Return the horizontal margin.
| ||||||||||
float |
getVerticalMargin()
Return the vertical margin.
| ||||||||||
View |
getView()
Return the view.
| ||||||||||
int |
getXOffset()
Return the X offset in pixels to apply to the gravity's location.
| ||||||||||
int |
getYOffset()
Return the Y offset in pixels to apply to the gravity's location.
| ||||||||||
static Toast |
makeText(
Context context, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.
| ||||||||||
static Toast |
makeText(
Context context,
CharSequence text, int duration)
Make a standard toast that just contains a text view.
| ||||||||||
void |
setDuration(int duration)
Set how long to show the view for.
| ||||||||||
void |
setGravity(int gravity, int xOffset, int yOffset)
Set the location at which the notification should appear on the screen.
| ||||||||||
void |
setMargin(float horizontalMargin, float verticalMargin)
Set the margins of the view.
| ||||||||||
void |
setText(int resId)
Update the text in a Toast that was previously created using one of the makeText() methods.
| ||||||||||
void |
setText(
CharSequence s)
Update the text in a Toast that was previously created using one of the makeText() methods.
| ||||||||||
void |
setView(
View view)
Set the view to show.
| ||||||||||
void |
show()
Show the view for the specified duration.
|
@默认的情况下:
private context mContext;
mContext=getApplicationContext();
String text="默认位置显示";
int duration="Toast.LENGTH_LONG/Toast.LENGTH_SHOW";
Toast toast=new Toast.makeText(mContext,text,duration).show();
如果你决定Toast应该出现在左上角,您可以设置重力是这样的:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
@@自定义显示位置的情况下:
private Toast toast;
toast=Toast.makeText(getApplicationContext(),"自定义位置显示",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
@@@带图片显示的情况下:
toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
@@@@完全自定义的情况下:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
@@@@@其他线程情况下:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
public void showToast(){
Toast toast=new Toast.makeText(mContext,"请输入:",Toast.LENGTH_SHOW).show();
}
注意:
不要使用公共构造函数一个Toast,除非你要定义布局withsetView(视图)。如果你没有使用一个自定义的布局,您必须使用makeText(上下文,int,int)创建吐司。
android4.0中控制Toast时间:
http://kakadu.blog.51cto.com/4050768/1019824