Toast 是一种提供给用户简洁提示信息的视图。
该视图以悬浮于应用程序之上的形式呈现给用户。Toast提示界面不获取焦点,所以不影响用户的操作。Toast提示就是在不影响用户使用程序的同时,给用户提供某些提示信息。有两个例子就是音量控制和设置信息保存成功提示。
Android提供的Toast类可以创建和显示该Toast信息
Toast显示信息分为:系统提供的Toast显示形式 和 自定义Toast
Toast 常用方法
Toast.makeText(context, text, duration);//返回值为Toast,
第一个参数为Toast所在的上下文, 第二个为Toast显示的文本信息, 第三个参数为Toast显示的时间
toast.setDuration(duration); //设置 持续时间
toast.setGravity(gravity, xOffset, yOffset);//设置toast位置
toast.setText(s); //设置提示内容
toast.show(); //显示
demo代码:
MainActivity.java
package com.example.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// TODO Auto-generated method stub
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.button1:
showToast1();
break;
case R.id.button2:
showToast2();
break;
case R.id.button3:
showToast3();
break;
case R.id.button4:
showToast4();
break;
}
}
//显示默认toast
private void showToast1() {
// TODO Auto-generated method stub
//Toast.makeText有两种形式,两者的不同在于第二个参数,第一种方法是一个String类型,
//第二种是一个int类型,通过得到XML中某个值得ID来获取对应的文本信息。
//这个函数的三个参数分别是:Toast所在的上下文, 显示的文本(直接赋予字符串,获给予字符串对应的ID,
//函数帮你找到显示相应的字符串) Toast显示时长(以毫秒为单位)
// Toast toast = Toast.makeText(this, "MainActivity", Toast.LENGTH_LONG);
Toast toast = Toast.makeText(this, R.string.app_name, 1000);
toast.show();
}
//自定义位置的Toast
private void showToast2() {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(this, "自定义位置的Toast", Toast.LENGTH_LONG);
//第一个参数是位置参数(居中、水平居中。。。) 第二个参数是x方向的偏移量,第三个是y方向的偏移量
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
//带图片的Toast
private void showToast3() {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(this, "带图片的Toast", Toast.LENGTH_LONG);
//先获取当前toast的布局
LinearLayout toast_layout = (LinearLayout)toast.getView();
ImageView image = new ImageView(this);
toast_layout.addView(image);
toast.show();
}
//自定义Toast
private void showToast4() {
// TODO Auto-generated method stub
//得到toast的自定义布局的View
//在系统中布局只能加载一次,
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.toast_layout, null);
Toast toast = new Toast(this);
toast.setView(view);
toast.show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="默认toast" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="22dp"
android:text="自定义位置的toast" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:layout_marginTop="26dp"
android:text="带有图片的toast" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="21dp"
android:text="自定义toast" />
</RelativeLayout>
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/image_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义Toast"
android:textSize="30sp"
android:gravity="center_vertical"/>
</LinearLayout>