Android中Toast详解

1,Android中的Toast是一种简易的消息提示框。当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。用户将可能是在中间键入别的东西。Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失。

2,Toast常用方法:

a)Toast.makeText(context,text,duration); //返回值为Toast

b)toast.setText(s); //设置提示内容

c)toast.setDuration(Duration); //设置持续时间

d)toast.setGravity(gravity,xOffset,yOffset); //设置toast位置

e)toast.show(); //显示

3,下面通过一个实例来讲解toast的不同方法的使用,用按钮1来显示默认的toast,按钮2显示改变位置的toast,按钮3显示带有图片的toast,按钮4显示完全自定义的toast。

4,四个按钮的布局文件:

<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        
        <Button 
            android:id="@+id/btn1"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="显示默认Toast"/>
        <Button 
            android:id="@+id/btn2"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="改变位置的Toast"/>
         <Button 
            android:id="@+id/btn3"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="显示图片的Toast"/>
          <Button 
            android:id="@+id/btn4"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="完全自定义的Toast"/>
    </LinearLayout>

</RelativeLayout></span>


5,完全自定义的toast的布局文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<TextView
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:gravity="center"
    android:text="这是自定义的Toast"
    android:textColor="#ff0000"
    android:textSize="20sp"/>
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/weather"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="内容部分,可以随便写"
    android:textColor="#00ff00"
    android:textSize="18sp"/>
</LinearLayout></span>


6,实现类中的代码:

<span style="font-size:18px;">package com.example.toastdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected  void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEvent();
    }
    /*
     * 初始化点击事件
     */
    private void initEvent(){
    findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

showToast1();
}
});
    findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

showToast2();
}
});
    findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

showToast3();
}
});
findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {

showToast4();
}
});
    }
    
    /*
     * 显示默认toast
     */
    private void showToast1(){
    Toast toast=Toast.makeText(this, "这是一个默认的Toast", Toast.LENGTH_SHORT);
    toast.show();
    }
    /*
     *显示自定义位置的Tooast
     */
    private void showToast2(){
    Toast toast=Toast.makeText(this, "这是一个自定义位置的Toast", Toast.LENGTH_SHORT);
    /*
    * 第一个参数用来设置显示位置
    * 第二个参数为水平偏移量
    * 第三个参数为垂直偏移量
    */
    toast.setGravity(Gravity.CENTER, 0, -200);
    toast.show();
    }
    /*
     * 显示带有图片的Toast
     */
    private void showToast3(){
    Toast toast=Toast.makeText(this, "带有图片的Toast", Toast.LENGTH_SHORT);
    //获取toast的布局
    LinearLayout toastLayout=(LinearLayout) toast.getView();
    //自定义一个imageView
    ImageView iView=new ImageView(this);
    iView.setImageResource(R.drawable.friends);
    /*
    * 把图片加载到toast布局上
    * 第二个参数是图片的索引(可以不设置,不设置时图片在文字的下方)
    */
    toastLayout.addView(iView,0);
    toast.show();
    }
    /*
     * 显示完全自定义的Toast
     */
    private void showToast4(){
    //获取自定义的layout
    LayoutInflater inflater=LayoutInflater.from(this);
    View toast_View=inflater.inflate(R.layout.toast_layout, null);
    //获取toast
    Toast toast=new Toast(this);
    toast.setView(toast_View);
    toast.show();  
    }
}</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值