Android Toast简介
Toast是Android中一种提供给用户简短信息的视图,该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能以不显眼的方式,使用户看到你提供的信息。显示的时间是有限制的,过一段时间后会自动消失,不过Toast本身可以控制显示时间的长短;
Toast的不同显示样式:







代码如下:
activity_main.xml
<LinearLayout 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"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button4" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button5" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button6" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Toast</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="button1">系统默认的Toast</string>
<string name="button2">自定义位置的Toast</string>
<string name="button3">只显示图片的Toast</string>
<string name="button4">显示图片和文字的Toast</string>
<string name="button5">自定义布局的Toast</string>
<string name="button6">其他线程的Toast</string>
<string name="text_view1">自定义布局的Toast</string>
<string name="text_view2">自定义布局:</string>
</resources>
toast.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:background="#708090"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_view2" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_view1" />
</LinearLayout>
MainActivity.java
package com.xiaozhang.wifi;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
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 {
private Toast toast = null;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new ButtonClick());
findViewById(R.id.button2).setOnClickListener(new ButtonClick());
findViewById(R.id.button3).setOnClickListener(new ButtonClick());
findViewById(R.id.button4).setOnClickListener(new ButtonClick());
findViewById(R.id.button5).setOnClickListener(new ButtonClick());
findViewById(R.id.button6).setOnClickListener(new ButtonClick());
}
public void showToast() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "其他线程的Toast",
Toast.LENGTH_SHORT).show();
}
});
}
class ButtonClick implements OnClickListener {
@SuppressLint("InflateParams")
@SuppressWarnings("static-access")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
toast.makeText(MainActivity.this, "默认的Toast", Toast.LENGTH_LONG)
.show();
break;
case R.id.button2:
// getApplicationContext()得到程序当前的默认Context
toast = Toast.makeText(getApplicationContext(), "自定义位置的Toast",
Toast.LENGTH_LONG);
// 设置Toast的位置
toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2,
toast.getYOffset() / 2);
toast.show();
break;
case R.id.button3:
toast = Toast.makeText(MainActivity.this, "只显示图片的Toast",
Toast.LENGTH_LONG);
ImageView img = new ImageView(MainActivity.this);
img.setImageResource(R.drawable.right);
toast.setView(img);
toast.show();
break;
case R.id.button4:
toast = Toast.makeText(getApplicationContext(), "显示图片和文字的Toast",
Toast.LENGTH_LONG);
LinearLayout layout = (LinearLayout) toast.getView();
ImageView img1 = new ImageView(getApplicationContext());
img1.setImageResource(R.drawable.right);
layout.addView(img1, 0);
toast.show();
break;
case R.id.button5:
// LayoutInflater作用是将layout的xml布局文件实例化为View类对象
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.toast, null);
Toast toast = new Toast(getApplicationContext());
// 在view中查找ImageView控件
ImageView image = (ImageView) view.findViewById(R.id.img);
image.setImageResource(R.drawable.right);
toast.setView(view);
toast.show();
break;
case R.id.button6:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
break;
}
}
}
}
PS:记得添加图片:right.png
参考自:http://liangruijun.blog.51cto.com/3061169/638913
Android Toast简介的更多相关文章
- 【译】Android系统简介—— Activity
续上一篇,继续介绍Android系统.上一篇: [译]Android系统简介 本文主要介绍构建Android应用的一些主要概念: Activity Activity是应用程序中一个单独的有UI的页面( ...
- Android插件简介
/** * @actor Steffen.D * @time 2015.02.06 * @blog http://www.cnblogs.com/steffen */ Android插件简介 Andr ...
- Appium Android Toast控件
Android Toast控件是Android系统级别的控件,不是App的控件,getPageSource是⽆法找到的. Toast介绍 1.背景 在安卓设备里面,使用各种手机应用程序的时候,需要先进 ...
- Android Toast cancel和show 不踩中不会知道的坑
说到Android Toast,几乎都很熟悉吧,下面讲讲怎么实现下面几种场景: 1.连续点击一个按钮,每次都产生一个新的Toast并且调用show方法 问题:触发了toast以后,toast内容会一直 ...
- Android Studio 简介及导入 jar 包和第三方开源库方[转]
原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...
- Android Toast效果设置
Android Toast效果设置 Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.总 ...
- "浅谈Android"第一篇:Android系统简介
近来,看了一本书,名字叫做<第一行代码>,是CSDN一名博主写的,一本Android入门级的书,比较适合新手.看了书之后,有感而发,想来进行Android开发已经有一年多了,但欠缺系统化的 ...
- Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装
Android Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- Android Toast效果
Android Toast效果是一种提醒方式,在程序中使用一些短小的信息通知用户,过一会儿会自动消失,实现如下: FirstActivity.java package org.elvalad.acti ...
随机推荐
- C++ 哈希表
从2011年开始,C++支持非排序的unordered_map和unordered_set(原先的map和set都是通过有序结构实现的) 下面是一些性能上的测试 #include<iostrea ...
- MD中bitmap源代码分析--设置流程
1. 同步/异步刷磁盘 Bitmap文件写磁盘分同步和异步两种: 1) 同步置位:当盘阵有写请求时,对应的bitmap文件相应bit被置位,bitmap内存页被设置了DIRTY标志.而在下发写请求给磁 ...
- 消除JavaScript闭包的一般方法
JavaScript 的闭包是一个其主动发展的特性, 也是一个被动发展的特性. 也就是说, 一方面, JS 有了闭包能更好解决一些问题. 另一方面, JS 为了解决某些问题, 而不得不使用闭包勉强来解 ...
- JVM运行原理及Stack和Heap的实现过程
Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行,而JVM是java的核心和基础,在ja ...
- lua中的坑
在工作中使用lua也有一年了,代码也写了不少,踩过不少坑,这里记录一下. table.sort table.sort是lua自带的排序函数,数据量小时,也还是不错的.不过要注意你传入的compare函 ...
- 白话C#:特性(转)
不管怎么样,转过来再说. http://www.kuqin.com/dotnet/20080628/10196.html 系列文章索引:<白话C#> 首先要说的是,可能一些刚接触C#的朋友 ...
- Highcharts 基本曲线图
基本曲线图实例 文件名:highcharts_line_basic.htm <html> <head> <meta charset="UTF-8" / ...
- POJ 3356 AGTC(最小编辑距离)
POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...
- JavaScript经典魔力代码
是什么使得JavaScript不同于其他程序设计语言,在浏览器修饰方面表现出其优异的特性?毫无疑问,JavaScript在Web应用领域受到的好评,既源于它自身灵活的动态特性,也源于浏览器对它充分的支 ...
- [Angular 2] *ngFor with index
Let's see how to track index when we use 'ngFor: <li *ngFor="#hero of heros | async, #i = in ...