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 ...
随机推荐
- oracle exp imp
oracle exp/imp
- linux系统下mySQL数据库 备份方法和脚本
数据库备份1.创建个备份存储目录mkdir /root/backup/2.以下内容写到dbbackup.sh #!/bin/bash cd /data/db_backup/mysqldump -uad ...
- 推荐:室内定位API - indoor Location API
indoor.rs 公司近日开放了API,包括免费free的,收费fee的版本. 详情见这里,价格不是很贵哦 Open API支持Android/iOS等移动平台 提供工具,帮助进行地图和WiFi信号 ...
- 10个利用Eclipse调试Java的常见技巧
http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...
- 解密UML九中关系
将UML中经常使用的九种关系分为了四组进行解释. 一.组合和聚合解说: 同样:均是指有部分组成总体. 不同:聚合是指能够独立存在的个体组成总体.(弱的拥有关系) 组合存在时间上的关系.总体和部分具有同 ...
- LSI SAS 2308配置操作
介绍LSISAS2308的配置操作 3.1 登录CU界面 介绍登录LSISAS2308的CU配置界面的方法. 3.2 创建RAID 介绍在LSISAS2308扣卡上创建RAID的操作方法. 3.3 配 ...
- openwrt time sycronize
三行命令搞定这个. opkg update opkg install ntpclient ntpclient -s -c 0 -h ntp.sjtu.edu.cn 最后把这个 放到 rc.local ...
- canvas--画宇宙
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 一个小的程序--实现中英文切换(纯css)
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- .NET Core初体验 - 在Mac下运行第一个Web示例程序
要说最近两天程序猿之间最喜欢吹水的事是什么?那绝壁是甲骨文要放弃Java!简直做梦都要笑醒!由于公司的产品线全面转向Java,最近几个月也一直在苦学Java技术.已经默默决定了,如果消息证实是真的,我 ...