`

Android中Toast的用法简介

阅读更多

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

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
29
30
31
32
33
<?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>

然后在程序中为三个按钮分别创建点击事件,代码如下:

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
29
30
31
32
33
34
35
36
37
38
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。
android-toast-usage-02
可以看到,最后一种图片加文字的显示显然是不符合我们的要求的,那么我们可以通过增加Toast的Layout来调整Toast上的布局方式。我们增加一个Toast的Layout描述xml文件,/res/layout/toast_layout.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
    }
});

再运行程序,显示如下:
android-toast-usage-03

另外,我们还可以使用setGravity()方法来定位Toast在屏幕上的位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。

分享到:
评论

相关推荐

    Android Toast各种使用方法及DEMO

    Android Toast各种使用方法及DEMO

    Android_Toast用法.docx

    Android_Toast用法

    Android 5.0以上Toast不显示的解决方法

    实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...

    PhoneGap android的Toast插件

    PhoneGap android的Toast插件,使用方法不用介绍了,用phoneGap开发android应用的人都知道,希望对大家有帮助。

    Android学习笔记之Button,Toast,menu的简单用法

    Android学习笔记之Button,Toast,menu的简单用法

    Android Notification Toast用法演示范例.rar

    Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...

    Android 演示简单toast和带图片toast的实现方法.rar

    Android 演示简单toast和带图片toast的实现方法,这些toast在平时的Android应用开发中使用频繁,本源码演示了两种最实用toast的用法,一种是不带图片,另一种是带图片:  // 简单的toast,不带图片的实现方法:  ...

    Android学习下 toast notification用法.rar

    一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。

    Toast用法详解(各种自定义Toast)实例

    android之Toast用法详解(各种自定义Toast)实例

    Android使用Toast显示消息提示框

    在前面的实例中,已经应用过Toast类来显示一个简单的提示框了。这次将对Toast进行详细介绍。Toast类用于在屏幕中显示一个消息提示框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一段时间后自动消失。...

    Android Toast的用法总结(五种用法)

    本篇文章主要介绍了Android Toast的用法总结(五种用法),android toast几种使用方法 toast经常会用到,今天做个总结,特别是自定义toast的布局,值得一看。

    android自定义Toast设定显示时间

    开发android的同学可能会抱怨Toast设定显示的时长无效...本文使用方法三进行实现,难度不大,直接看代码吧. package com.open.toast; import android.content.Context; import android.graphics.Color; import andro

    Android Toast用法代码实例

    摘要:Java源码,Android,Toast,Android源码 Android Toast用法代码实例,建立属于你... Toast在Android系统中用于向用户显示一些帮助/提示,本源码是一个较基本的Toast用法实例,还望通过本例能加深您对Toast的理解。

    Android Toast通知用法实例详解

    本文实例讲述了Android Toast通知用法。分享给大家供大家参考,具体如下: Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。 1.默认用法 代码如下:Toast.makeText(getApplicationContext(), ...

    Android 自定义 Toast 显示时间

    Android 自定义 Toast 显示时间 实现代码: ... import android.content.... * 使用方法 * 1.先初始化类 MyToast myToast = new MyToast(this); * 2.显示消息 myToast.setText(要显示的内容); //设置要显示的内容 *

    Android Toast自定义显示时间

    常规使用方法这里不做说明,继前一篇博客《Android中Toast全屏显示》 ,其中抛砖引玉的给出一个简单的实现Toast全屏显示的方法后,发现无法控制Toast的显示时长。虽然Toast中有setDuration(int duration)接口,但是...

    toast几种用法

    最近学习的toast用法,包括自定义位置toast和带图片的toast

    自定义Toast的显示内容和显示位置

    Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”...

    Android中使用Toast.cancel()方法优化toast内容显示的解决方法

    看到Toast有一个cancel()方法: 代码如下:void cancel() Close the view if it’s showing, or don’t show it if it isn’t showing yet. 做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后...

    android之自定义Toast使用方法

    使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局,例如: 代码如下: &lt;?xml version=”1.0″ encoding=”utf-8″?&...

Global site tag (gtag.js) - Google Analytics