Android 2.2 r1 API中文文档—— Toast

Android 2.2 r1 API中文文档——

Toast

 By jiahuibin

Emailhuibin.jia@gmail.com

2010-11-3

一、类结构

         Toast extends Object

         java.lang.Object

                          android.widget.Toast

二、概述

       Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示这些。

       当这个视图被显示给用户时,是一种漂浮在应用程序上的视图。它不获得焦点。用户可能在屏幕中间输入一些其他信息什么的。这种方式不是特别突兀,他会在当你想显示的时候一直显示。例如“音量控制提示”,还有“设置生效的提示”就是toast比较好的两个例子。

       最简洁的方式就是用这个类调用一种静态方法去构建你需要的东西和返回一个新的toast对象。

       下面就是toast的效果图,后面的例子中的代码可以很好的为大家解释如何实现这些。

       Toast主要用来提示用户,以实现友好的用户体验,这里给出两个Toast的例子:
1
、使用图片

Toast toast = new Toast(this); 

ImageView view = new ImageView(this); 

view.setImageResource(R.drawable.icon); 

toast.setView(view); 

toast.show();
2
、使用文字对话框

private void showToast() {

// 1 创建Toast

              Toast toast = Toast.makeText(this, "图文显示", Toast.LENGTH_LONG);

// 2 创建Layout,并设置为水平布局

              LinearLayout mLayout = new LinearLayout(this);

              mLayout.setOrientation(LinearLayout.HORIZONTAL);

              ImageView mImage = new ImageView(this); // 用于显示图像的ImageView

              mImage.setImageResource(R.drawable.icon);

              View toastView = toast.getView(); // 获取显示文字的Toast View

              mLayout.addView(mImage); // 添加到Layout

              mLayout.addView(toastView);

// 3 关键,设置Toast显示的View(上面生成的Layout).

              toast.setView(mLayout);

              toast.show();

       }

三、大纲

 

常量

int

LENGTH_LONG

显示图片或者文本提示信息较长一段时间.

int

LENGTH_SHORT

显示图片或者文本提示信息

 

 

 

 

 

 

 

 

构造方法

Toast(Context context)    构建一个空的Toast对象

 

 

公共方法

void

cancel()

toast显示时关闭它,或者在还没有显示,不让他显示。

int

getDuration()

返回持续时间。

int

getGravity()

得到这个提示信息显示在屏幕中的位置。

float

getHorizontalMargin()

返回横向页边距

float

getVerticalMargin()

返回纵向页边距

View

getView()

返回视图

int

getXOffset()

返回Gravity位置的x偏移位置。

int

getYOffset()

返回Gravity位置的y偏移位置。

static Toast

makeText(Context context, int resId, int duration)

标准的文本toast,这个toast中含有的文本来自资源文件。

static Toast

makeText(Context context, CharSequence text, int duration)

标准的视图toast,这个tosat里面含有文本视图。

void

setDuration(int duration)

设置显示时间。

void

setGravity(int gravity, int xOffset, int yOffset)

设置toast的布局位置,以及相对这个位置的偏移量。

void

setMargin(float horizontalMargin, float verticalMargin)

Set the margins of the view.

void

setText(int resId)

更新先前由方法makeText()创建的toast中的文本

void

setText(CharSequence s)

更新先前由方法makeText()创建的toast中的文本

void

setView(View view)

设置那个视图显示。

void

show()

显示这个视图指定的时间

四、常量

public static final int LENGTH_LONG

显示view或者text提示信息一段较长时间,这个时间用户可以自定义。

参照:

·               setDuration(int)

Constant Value: 1 (0x00000001)

public static final int LENGTH_SHORT

显示view或者text提示信息一段较长时间,这个时间用户可以自定义。

参照

·               setDuration(int)

常量值: 0 (0x00000000)

 

五、构造方法

public Toast (Context context)

构建一个空的Toast对象。你在使用show()方法前,必须先调用setView(View)

注意:只有使用setView(View)的时候,才使用new Toast(Content content)来得到Toast对象,否则必须用makeText()方法来创建toast对象,并且这种方式获得Toast对象不能使用setText()方法

参数

context

上下文. 通常是你的应用程序或者是Activity对象.

六、公共方法

public void cancel ()

假如这个视图在显示,关闭它,如果还没有显示,将不让它显示。通常情况下我们不会调用它,因为这个视图自己会在适当的时间后消息。

public int getDuration ()

返回持续时间

参照

·               setDuration(int)

public int getGravity ()

得到这个提示信息在屏幕中的布局位置。

参照

·               Gravity

·               getGravity()

public float getHorizontalMargin ()

返回横向页边距

public float getVerticalMargin ()

返回纵向页边距

public View getView ()

返回要显示的视图

参照

·               setView(View)

public int getXOffset ()

返回这个布局位置的x偏移。下面是这个方法的妙用,把toast居中显示

Toast msg = Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();

 

public int getYOffset ()

返回这个布局位置的y偏移。

public static Toast makeText (Contextcontext, int resId, int duration)

标准的文本toast,这个toast中含有的文本来自资源文件。

Parameters

context

上下文. 通常是你的应用程序或者是Activity对象.

resId

引用字符串id.

Duration

这个消息显示的时间长度。要么是LENGTH_SHORT要么就是LENGTH_LONG

Throws

 

如果那个源找不到抛出异常.

Resources.NotFoundException

 

public static Toast makeText (Contextcontext, CharSequence text, int duration)

标准的视图toast,这个tosat里面含有文本视图。

Parameters

context

上下文. 通常是你的应用程序或者是Activity对象.

text

要显示的文本。

duration

这个消息显示的时间长度。要么是LENGTH_SHORT要么就是LENGTH_LONG

public void setDuration (int duration)

设置view显示多长时间。

See Also

·               LENGTH_SHORT

·               LENGTH_LONG

public void setGravity (int gravity, int xOffset, int yOffset)

设置toast的布局位置,以及相对这个位置的偏移量。

例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。

Toast提示的位置xOffset:大于0向右移,小于0向左移

参照

·               Gravity

·               getGravity()

public void setMargin (float horizontalMargin, float verticalMargin)

设置视图的页边距.

参数

horizontalMargin

左右页边距,容器边缘和toast边缘距离。

verticalMargin

上下页边距,容器边缘和toast边缘距离。

public void setText (int resId)

更新先前由方法makeText()创建的toast中的文本

参数

resId

新的字符的id.

public void setText (CharSequence s)

更新先前由方法makeText()创建的toast中的文本

参数

s

新的字符串对象.

public void setView (View view)

设置要显示的视图,

注意:这个方法可以显示自定义的toast视图,可以包含图像,文字等等。是比较常用的方法。

参照

·               getView()

public void show ()

显示这个视图指定的时间

六、代码

       我们的代码没有重复概述中的通过代码布局toast实现方法,我们是通过布局文件转换成view视图来实现复杂toast,这是两种常用的方法,大家可以根据具体情况进行选择

Xml布局文件

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/button1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="Toast显示View"

/>

<Button android:id="@+id/button2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="Toast直接输出"

/>

<Button android:id="@+id/button3"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="VolumeToast应用"

/>

</LinearLayout>

Toast.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"

    >

<ImageView android:src="@drawable/toast"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

/>

<TextView android:id="@+id/tv1"

    android:text=""

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

/>

</LinearLayout>

Volumetoast.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="280dp"

  android:layout_height="wrap_content"

  android:gravity="center_horizontal"

  android:orientation="vertical"

  >

          <TextView          

                  android:layout_width="fill_parent"

                  android:layout_height="wrap_content"

                  android:text="Volume"

          />

          <ProgressBar

                  android:id="@+id/progress"

                  android:layout_width="280dp"

                  android:layout_height="wrap_content"

                  android:progress="50"

                  android:max="100"

                  style="?android:attr/progressBarStyleHorizontal"

          />

</LinearLayout>

 

 

Java代码文件

package com.jhb.toasttest;

 

import android.app.Activity;

import android.os.Bundle;

import android.content.Context;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

 

public class toasttest extends Activity {

   

       /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button button1=(Button)findViewById(R.id.button1);

        button1.setOnClickListener(bt1lis);

        Button button2=(Button)findViewById(R.id.button2);

        button2.setOnClickListener(bt2lis);

        Button button3=(Button)findViewById(R.id.button3);

        button3.setOnClickListener(bt3lis);

    }

    OnClickListener bt1lis=new OnClickListener(){

 

              @Override

              public void onClick(View v) {

                     showToast();

              }

 

    };

    OnClickListener bt2lis=new OnClickListener(){

              @Override

              public void onClick(View v) {

                     Toast.makeText(toasttest.this,"直接输出测试", Toast.LENGTH_LONG).show();

              }

 

    };

    OnClickListener bt3lis=new OnClickListener(){

              @Override

              public void onClick(View v) {            

                     showvolumeToast();

              }

 

    };

    public void showToast(){

           LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

           View view=li.inflate(R.layout.toast,null);

           //把布局文件toast.xml转换成一个view

           Toast toast=new Toast(this);

           toast.setView(view);

           //载入view,即显示toast.xml的内容

           TextView tv=(TextView)view.findViewById(R.id.tv1);

           tv.setText("Toast显示View内容");

           //修改TextView里的内容

           toast.setDuration(Toast.LENGTH_SHORT);

           //设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑

           toast.show();

    }

       public void showvolumeToast() {

              // TODO Auto-generated method stub

             LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

           View volumeView=li.inflate(R.layout.volumetoast,null);

        ((ProgressBar)volumeView.findViewById(R.id.progress)).setProgress(((ProgressBar)volumeView.findViewById(R.id.progress)).getProgress() + 10);

        //这里还有点问题,就是progress的进度条不动,因为我们主要是想给大家展示

     //Toast的用法,这里就不深究了

        Toast volumeToast= new Toast(this);

        volumeToast.setGravity(Gravity.BOTTOM, 0, 100);

        volumeToast.setView(volumeView);

        volumeToast.setDuration(Toast.LENGTH_SHORT);      

        volumeToast.show();

       }

}

七、toast不足之处

      现象:当点击一个按钮 可以显示一个四秒的toast,但是我要是连点两下就是8秒 三下十二秒

      解决办法:只用一个Toast, 自己设置toast.setText(), setDuration(); 之后无论多少次.show()都能马上更新显示, 一会就消失了

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值