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 ...
随机推荐
- 关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌!
关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌! HerlMax品牌拥有着纯正悠远的意大利高级定制文化,带着对神秘东方的向往,荣耀登入中国市场.
- Linux+eclipse+gdb调试postgresql源码
pg内核源码解析课上用的vs调试pg源码, VS用起来确实方便,但是配置调试环境着实有点麻烦.首先得装个windows系统,最好是xp,win7稍微麻烦点:最好使用vs05,08和10也可以,但是比0 ...
- apue
#ifndef apue_h #define apue_h #define _POSIX_C_SOURCE 200809L #if defined(SOLARIS) /* Solaris 10 */ ...
- nginx本地的测试环境添加SSL
要在本地添加SSL,首先要做的是防火墙是不是放开了443端口,同时,在nginx安装时是不是支持了ssl模块,这个安装网上很容易找到相关资料 防火墙,个人还是用iptables比较直观 先将selin ...
- CSS3:优雅地绘制不规则ICON
早上在w3ctech上看到 中国第二届CSS Conf总结 的时候,真是开心极了: 自从去年在慕课网上看了第一届CSS conf 视频之后,整个人都震惊了,原来还有大会是专门用来讨论CSS的,而且分 ...
- 怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障
怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障 英文原文引自 http://xphelpandsupport.mvps.org/how_do_i_repa ...
- Qt的Graphics-View框架和OpenGL结合详解
Qt的Graphics-View框架和OpenGL结合详解 演示程序下载地址:这里 程序源代码下载地址:这里 这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果. Qt中有一个非常炫的例子:Boxe ...
- How To Make a Music Visualizer in iOS
Xinrong Guo on June 4, 2013 Tweet Learn how to create your own music visualizer! In the mid-seventi ...
- BitmapFactory 加载图片到内存
Bitmap占用内存分析 Android的虚拟机是基于寄存器的Dalvik,它的最大堆(单个进程可用内存)大小一般是16M,当然不同设备是不一样的,可以查看/system/build.prop文件,[ ...
- DataSet ,DataTable,DataRow 之间的关系与使用
关系 DataSet 包含多个DataTable,DataTable包含多行DataRow. 使用情况: 有时候GridView等控件需要将数据源动态绑定到DataSet中:将多个DataSe ...