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定位在左上角。

Toast是一种轻量级的提示框,用于在屏幕上显示简短的消息。以下是AndroidToast用法: 1. 在布局文件定义Toast的布局样式,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#FFFFFF" android:padding="8dp"> <ImageView android:id="@+id/toast_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!"/> </LinearLayout> ``` 2. 在Java代码创建Toast对象,并设置其显示的位置和时长等属性,例如: ```java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, null); ImageView icon = layout.findViewById(R.id.toast_icon); TextView text = layout.findViewById(R.id.toast_text); icon.setImageResource(R.drawable.ic_launcher); text.setText("Hello world!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); ``` 以上代码,通过LayoutInflater从XML布局文件加载Toast的布局样式,然后设置图标和文本内容,并通过setGravity()方法设置Toast的显示位置,最后调用show()方法显示Toast。 注意:Toast必须在UI线程显示。如果在非UI线程显示Toast,需要使用Handler或runOnUiThread()方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值