Toast用法

来源:互联网 发布:键盘windows键解锁 编辑:程序博客网 时间:2024/05/16 01:29

Toast窗口,在其窗口中可以显示文本以及图像,,不能获得焦点,在显示一定时间后会自动关闭。下面介绍其用法

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
//定义字符量
private static final String TOASTBIN_1="常用的Toast";
private static final String TOASTBIN_2="自定义位置的Toast";
private static final String TOASTBIN_3="图片的Toast";
private static final String TOASTBIN_4="自定义的Toast";
private static final String TOASTBIN_5="长时间的Toast显示";
//定义按钮
private Button button_1,button_2,button_3,button_4,button_5;
private Toast toast =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_1=(Button)findViewById(R.id.button_1);
button_2=(Button)findViewById(R.id.button_2);
button_3=(Button)findViewById(R.id.button_3);
button_4=(Button)findViewById(R.id.button_4);
button_5=(Button)findViewById(R.id.button_5);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

//建立builder dialog对象
AlertDialog.Builder builder;
AlertDialog dialog;
switch(v.getId())
{
case R.id.button_1:
toast.makeText(this, TOASTBIN_1, toast.LENGTH_LONG).show();
break;
case R.id.button_2:
toast=Toast.makeText(getApplicationContext(), TOASTBIN_2, Toast.LENGTH_LONG);
//用setGravity自己定义位置
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case R.id.button_3:
/*
* getApplicationContext() 返回应用的上下文,生命周期是整个应用,应用摧毁它才摧毁
               Activity.this的context 返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁
               getBaseContext()  返回由构造函数指定或setBaseContext()设置的上下文
*/
toast=Toast.makeText(getApplicationContext(), TOASTBIN_3, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 50, -100);
LinearLayout layout =(LinearLayout) toast.getView();
ImageView image=new ImageView(getApplicationContext());
image.setImageResource(R.drawable.tupian);
//动态加载布局
layout.addView(image,0);
toast.show();
break;
case R.id.button_4:
LayoutInflater inflater =getLayoutInflater();
View view=inflater.inflate(R.layout.ut, (ViewGroup)findViewById(R.id.toast_layout));
//ut界面布局中标题
TextView textView_Title=(TextView) view.findViewById(R.id.txt_Title);
//ut界面布局中内容
TextView txtView_ConText=(TextView)view.findViewById(R.id.txt_context);
ImageView imageView=(ImageView) view.findViewById(R.id.image_toast);
toast=new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
break;
case R.id.button_5:
LayoutInflater inflater1=getLayoutInflater();
View view1=inflater1.inflate(R.layout.ut, (ViewGroup)findViewById(R.id.toast_layout));
TextView txtView_Title1=(TextView) view1.findViewById(R.id.txt_Title);
TextView txtView_Context1=(TextView)view1.findViewById(R.id.txt_context);
ImageView imageView1 =(ImageView)view1.findViewById(R.id.image_toast);
builder=new AlertDialog.Builder(this);
builder.setView(view1);
dialog=builder.create();
dialog.show();
break;
default:
break;
}
}
}


布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button 
  android:id="@+id/button_1"
  android:text="@string/toast_btn_1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  ></Button>
  <Button 
  android:id="@+id/button_2"
  android:text="@string/toast_btn_2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  ></Button>
  <Button 
  android:id="@+id/button_3"
  android:text="@string/toast_btn_3"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  ></Button>
  <Button 
  android:id="@+id/button_4"
  android:text="@string/toast_btn_4"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  ></Button>
  <Button 
  android:id="@+id/button_5"
  android:text="@string/toast_btn_5"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  ></Button>
</LinearLayout>


toast窗口布局文件

ut.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:background="#111111"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:text="美好世界"
android:textColor="#ffffff"
android:textSize="20dip" >
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999999"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:src="@drawable/tupian" >
</ImageView>
<TextView
android:id="@+id/txt_context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="魅力图片"
android:textColor="#ffffff"
android:textSize="15dip" >
</TextView>
</LinearLayout>
</LinearLayout>


通过以上程序演示可以知道,Toast方法实际上是通过一个view对象来显示消息的,当一个按钮绑定多个dialog时,Toast窗口是按顺序显示的











0 0