Android中Toast的用法简介

ToastAndroid 中用来显示显示信息的一种机制,和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 定位在左上角。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值