Android Toast使用简介

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。
首先建立一个ToastExample的项目,放置3个按钮,分别为Text Only,Icon Only,Text and Icon。布局及XML如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:text="Text Only"
android:id="@+id/Button01"
android:layout_width="100px"
android:layout_height="wrap_content"
/>
<Button
android:text="Icon Only"
android:id="@+id/Button02"
android:layout_width="100px"
android:layout_height="wrap_content"
/>
<Button
android:text="Icon and Text"
android:id="@+id/Button03"
android:layout_width="120px"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
然后在程序中为三个按钮分别创建点击事件,代码如下:

Button btn;
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Text toast test!", Toast.LENGTH_LONG).show();
}
});
btn = (Button) findViewById(R.id.Button02);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast toast = new Toast(getApplicationContext());
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.ic_dialog_alert);
toast.setView(view);
toast.show();
}
});
btn = (Button) findViewById(R.id.Button03);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast toast = Toast.makeText(getApplicationContext(), "Text and Icon test!", Toast.LENGTH_LONG);
View textView = toast.getView();
LinearLayout lay = new LinearLayout(getApplicationContext());
lay.setOrientation(LinearLayout.HORIZONTAL);
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.ic_dialog_alert);
lay.addView(view);
lay.addView(textView);
toast.setView(lay);
toast.show();
}
});
然后运行程序分别点击三个按钮可以看到三种不同情况下的Toast。

可以看到,最后一种图片加文字的显示显然是不符合我们的要求的,那么我们可以通过增加Toast的Layout来调整Toast上的布局方式。我们增加一个Toast的Layout描述xml文件,/res/layout/toast_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
然后我们修改Button3的OnClick事件:

btn = (Button) findViewById(R.id.Button03);
btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_dialog_alert);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
再运行程序,显示如下:


另外,我们还可以使用setGravity()方法来定位Toast在屏幕上的位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值