3 使用Toast动态显示信息

3-1 重新认识Toast (01:48)

一、toast
  1.Toast是一种提供给用户简洁提示信息的视图。
  2.该视图以浮于应用程序之上的形式呈现给用户。Toast提示界面不获取焦点,所以不影响用户的操作。Toast提示就是在不影响用户使用呈现的同时,给用户提供某些提示信息。有两个例子就是音量控制和设置信息保存成功。
  3.Android提供的Toast类可以创建和显示该Toast信息。

二、Toast常用方法
  Toast.makeText(context, text, duration);//返回值为Toast
  Toast.setDuration(duration);//设置持续时间
  Toast.setGravity(gravity, xOffset, yOffset);//设置toast位置
  Toast.setText(s);//设置提示内容
  Toast.show();//显示

  ps.Toast.setDuration()里面的参数,可以是静态的Toast.LENGTH_LONG和Toast.LENGTH_SHORT,同时,其实可以直接用1和0代替,
  因为Toast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应1和0

 

3-2 显示默认的Toast (05:31)

case R.id.button_1:

toast.makeText(this, TOASTBTN_1, Toast.LENGTH_LONG).show();

break;

 

3-3 改变Toast的显示位置 (04:36)

/**
* 显示自定义位置的Toast
*/
private void showToast2(){
Toast toast = Toast.makeText(this,"改变位置的Toast!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

xOffset设置正值则向右偏移,设置负值则向左偏移
yOffset设置正值则向下偏移,设置负值则向上偏移

3-4 显示带图片的Toast (02:58)

设置带图片的Toast:
// 通过这种方法来初始化toast
Toast toast = Toast.makeText(MainActivity.this,"这是显示带有图片的toast", 2000);
// 将toast强制转换为布局文件
LinearLayout ll = (LinearLayout) toast.getView();
// 初始化ImageView的控件,并设置控件中的图片资源
ImageView iv = new ImageView(MainActivity.this);

//ImageView添加图片
iv.setImageResource(R.drawable.ic_launcher);
// 在布局文件中添加这个ImageView的控件,addView的第二个参数是为这个图片设置位置,为0的时候表示在文字的上面,toast布局添加ImageView,如果没有0,图片在文字下面
ll.addView(iv, 0);
// show出来
toast.show();

 

3-5 自定义Toast (05:15)

第一步:创建布局文件LinearLayout;
第二步:创建LayoutInflater对象,并赋值为LayoutInflater.from(this);
第三步:创建View对象,赋值为inflater.inflate(R.layout...,null);(其中R.layout...为第一步中的布局文件)
第四步:new出Toast对象
第五步:调用Toast的setView方法加载View对象,show出即可;

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
获取LayoutInflater的方法有如下三种:
?
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);

LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
View layout = inflater.inflate(R.layout.main, null);

LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main, null);

代码如下:

//初始化toast
Toast toast = new Toast(this);

// 先获得inflater
LayoutInflater inflater = LayoutInflater.from(this);

// 通过inflater来将我们自定义的toast的布局转化为view的控件
View toast_view = inflater.inflate(R.layout.toast_layout, null);

// 通过toast的setView的方法将我们上面得到的view添加到toast中去
toast.setView(toast_view);

// 调用show的方法来实现toast的显示
toast.show();

 

整体代码:

MainActivity.java

package com.imooc.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
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.toast_btn1).setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showToast1();
            }
        });
        findViewById(R.id.toast_btn2).setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showToast2();
            }
        });
        findViewById(R.id.toast_btn3).setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showToast3();
            }
        });
        findViewById(R.id.toast_btn4).setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showToast4();
            }
        });
    }
    /**
     * 显示默认toast
     */
    private void showToast1(){
        //Toast toast = Toast.makeText(this,"这是一个默认的Toast!",Toast.LENGTH_SHORT);
        Toast toast = Toast.makeText(this,R.string.app_name,Toast.LENGTH_LONG);
        toast.show();
    }
    /**
     * 显示自定义位置的Toast
     */
    private void showToast2(){
        Toast toast = Toast.makeText(this,"改变位置的Toast!",Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
    /**
     * 带有图片的toast;
     */
    private void showToast3(){
        Toast toast = Toast.makeText(this,"带有图片的Toast!",Toast.LENGTH_LONG);
        LinearLayout toast_layout = (LinearLayout)toast.getView();
        ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.topimg);
        toast_layout.addView(iv,0);
        toast.show();
    }
    /**
     * 完全自定义的Toast
     */
    private void showToast4(){
        LayoutInflater inflater = LayoutInflater.from(this);
        View toast_view = inflater.inflate(R.layout.toast_layout, null);
        Toast toast = new Toast(this);
        toast.setView(toast_view);
        toast.show();
    }
}

activity_main.xml

<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:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn1"
            android:text="显示默认Toast"
            />
         <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn2"
            android:text="改变位置的Toast"
            />
         <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn3"
            android:text="显示图片的Toast"
            />
         <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn4"
            android:text="完全自定义的Toast"
            />
        </LinearLayout>
</RelativeLayout>

toast_layout.xml

<?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="30dip"
        android:gravity="center"
        android:text="这个是自定义的Toast!"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/topimg"
        />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="内容部分,我们可以随便写!"
        />
</LinearLayout>

 

posted @ 2016-04-12 11:53  沉默的羊癫疯  阅读(449)  评论(0编辑  收藏  举报