Android Toast 总结(五种用法)

Toast大家都很熟,不多说。直接上图上代码。

      

            

具体代码如下:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dip" >

    <Button
        android:id="@+id/btnSimpleToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="默认" >
    </Button>

    <Button
        android:id="@+id/btnSimpleToastWithCustomPosition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自定义显示位置" >
    </Button>

    <Button
        android:id="@+id/btnSimpleToastWithImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="带图片" >
    </Button>

    <Button
        android:id="@+id/btnCustomToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="完全自定义" >
    </Button>

    <Button
        android:id="@+id/btnRunToastFromOtherThread"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="其他线程" >
    </Button>

</LinearLayout>

custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llToast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvTitleToast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dip"
        android:background="#bb000000"
        android:gravity="center"
        android:textColor="#ffffffff" />

    <LinearLayout
        android:id="@+id/llToastContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:background="#44000000"
        android:orientation="vertical"
        android:padding="15dip" >

        <ImageView
            android:id="@+id/tvImageToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/tvTextToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textColor="#ff000000" />
    </LinearLayout>

</LinearLayout>
package com.example.test;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity implements OnClickListener {
    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.btnSimpleToast).setOnClickListener(this);
        findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
                this);
        findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
        findViewById(R.id.btnCustomToast).setOnClickListener(this);
        findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

    }

    public void showToast() {
        handler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "我来自其他线程!",
                        Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(View v) {
        Toast toast = null;
        switch (v.getId()) {
        case R.id.btnSimpleToast:
            Toast.makeText(getApplicationContext(), "默认Toast样式",
                    Toast.LENGTH_SHORT).show();
            break;
        case R.id.btnSimpleToastWithCustomPosition:
            toast = Toast.makeText(getApplicationContext(), "自定义位置Toast",
                    Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            break;
        case R.id.btnSimpleToastWithImage:
            toast = Toast.makeText(getApplicationContext(), "带图片的Toast",
                    Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            LinearLayout toastView = (LinearLayout) toast.getView();
            ImageView imageCodeProject = new ImageView(getApplicationContext());
            imageCodeProject.setImageResource(R.drawable.ic_launcher);
            toastView.addView(imageCodeProject, 0);
            toast.show();
            break;
        case R.id.btnCustomToast:
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.custom,
                    (ViewGroup) findViewById(R.id.llToast));
            ImageView image = (ImageView) layout
                    .findViewById(R.id.tvImageToast);
            image.setImageResource(R.drawable.ic_launcher);
            TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
            title.setText("Attention");
            TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
            text.setText("完全自定义Toast");
            toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
            break;
        case R.id.btnRunToastFromOtherThread:
            new Thread(new Runnable() {
                public void run() {
                    showToast();
                }
            }).start();
            break;

        }

    }

}

运行即可。

本文转载自:《自定义Toast

时间: 2024-08-06 22:46:52

Android Toast 总结(五种用法)的相关文章

Android系统的五种数据存储形式(一)

Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频数据.XML注册文件的各种数据.各种存储形式的特点不尽相同,因此对于不同的数据类型有着固定的存储形式,本文为演示方便给出的案例基本相同,都是是采用账号登录来演示数据存储,保存账号和密码信息,下次登录时记住账号和密码.重在说明各种存储形式的原理. 文件存储: 以I/O流的形式把数据存入手机内存或SD卡

C#this的五种用法

this的五种用法: 1.使用被掩盖的成员变量: class AA { int a; public void set1(int a) { this.a = a;//right } public void set2(int a) { a = a;//会有警告:“对同一变量进行赋值:是否希望对其他变量赋值?”: } } 2.把这个对象传给其他函数 using System; using System.Collections.Generic; using System.Linq; using Syst

Android 进程的五种生命周期学习

本节学习进程的生命周期: Android系统是尽可能的去保护每一个进程,但是最终需要为新的进程,或者很重要的进程释放以前的老进程.为了决定那个进程被保护,那个被杀死.Android系统根据当前进程中组件的状态,以及运行在进行中的组件决定保留那个,杀死那个进程.当然了系统资源短缺时,进程等级低的先杀死,以此类推. android系统中有五种进程等级: 1: 前台进程(前台进程有五种状态,只有其中一种满足就是前台进程,前台进程是很难被杀死的) a:拥有一个正在与用户交互的Activity(此时Act

Android数据存储五种方式总结

本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用ContentProvider存储数据 5 网络存储数据 下面详细讲解这五种方式的特点 第一种: 使用SharedPreferences存储数据     适用范围:保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配置信息(如是否打开音效.是否使用震动效果.小游戏的玩家积分

Android系统的五种数据存储形式(二)

之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所以这里还是将这两种形式简要的说明一下. Content Provider: Content Provider,中文名是内存提供者,Android四大组件之一,内容提供者是应用程序之间共享数据的接口,以数据库形式存入手机内存,可以共享自己的数据给其他应用使用.之所以需要设计一个单独的控件来操作数据,是

Android常用的五种弹出对话框

一个Android开发中常用对话框的小例子,共有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

Android 数据存储五种方式

1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用SharedPreferences存储数据 ② 文件存储数据 ③ SQLite数据库存储数据 ④ 使用ContentProvider存储数据 ⑤ 网络存储数据 Android提供了一种方式来暴露你的数据(甚至是私有数据)给其他应用程序 - ContentProvider.它是一个可选组件,可公开读写你应用程序数

android数据的五种存储方式

Android提供了5种方式存储数据 (1)使用SharedPreferences存储数据,它是Android提供的用来存储一些简单配置信息的一种机制,采用了XML格式将数据存储到设备中.只能在同一个包内使用,不能在不同的包之间使用. SharedPreferences存储方式,它是Android提供的用来存储一些简单配置信息的一种机制,例如:登录用户的用户名与密码.其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入.存储目录/data/data/Package Name/

[Android] 数据存储五种方式使用与总结

1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用SharedPreferences存储数据 ② 文件存储数据 ③ SQLite数据库存储数据 ④ 使用ContentProvider存储数据 ⑤ 网络存储数据 Android提供了一种方式来暴露你的数据(甚至是私有数据)给其他应用程序 - ContentProvider.它是一个可选组件,可公开读写你应用程序数