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 ...
随机推荐
- 从一个聊天信息引发的思考之Android事件分发机制
转载请声明:http://www.cnblogs.com/courtier/p/4295235.html 起源: 我在某一天看到了下面的一条信息(如下图),我想了下(当然不是这 ...
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- 如何避免被C++默认拷贝构造函数忽悠?
一.背景介绍 因为工作关系,需要用到C++编程.对于我来说,虽然一直从事的是linux平台下的嵌入式软件开发,但深入用到C++的特性的地方并不多.对于C++,用得最多的无非是指针. ...
- Servlet问题:servlet cannot be resolved to a type解决办法
工程里的路径权限高,并且eclipse并到classpath里寻找jar位置,所以我就到我的java项目里 项目名-->右键 Property-->选择 Java Build Path-- ...
- @SuppressWarnings(unchecked)作用解释
解释一: 屏蔽某些编译时的警告信息 在强制类型转换的时候编译器会给出警告 加上 程序代码 @SuppressWarnings("unchecked") 就不会警告了 解释二: 注释 ...
- 300元差价选谁好 魅蓝note对比魅蓝手机
http://mobile.pconline.com.cn/608/6089437.html [PConline 对比评测]999元的魅蓝note和699元的魅蓝手机先后被发布,代表着魅族中低端手机已 ...
- Android学习路线(二十七)键值对(SharedPreferences)存储
假设你又一个相对较小的键值对数据想要保存,你应该使用SharedPreferences APIs.一个SharedPreferences 对象指向一个包括键值对的文件,它提供简单的方法来读写他们.每一 ...
- [React Testing] Intro to Shallow Rendering
In this lesson, we walk through how to use one of React's Test Utilities (from thereact-addons-test- ...
- i2c sub system __i2c_board_list/klist_devices/klist_drivers
i2c_devinfo全局链表: __i2c_board_list 用来挂接 i2c_board_info,这个信息用来生成 i2c_client i2c_client 链表: i2c_bus_typ ...
- Solr的安装
1. JDK要求 Solr 4.10 要求JDK版本必须是1.7或更高. 下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/ 下载得到z ...