一、什么是Toast

1、Toast是一种提供给用户简洁提示信息的视图。

2、该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示。

3、Android提供的Toast类可以创建和显示Toast信息。

下面演示4种Toast的用法,普通的 Toast,更改位置的Toast,显示图片的Toast,自定义的Toast分别使用4个按钮来实现。

main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/btnShow"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Toast显示"/>
  12.  
  13. <Button
  14. android:id="@+id/btnShow2"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="Toast显示位置"/>
  18.  
  19. <Button
  20. android:id="@+id/btnShow3"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="Toast显示图片"/>
  24.  
  25. <Button
  26. android:id="@+id/btnShow4"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="自定义Toast显示layout文件"/>
  30.  
  31. </LinearLayout>

main.java

  1. package com.example.zhengcheng.myapp;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15. Button btnshow1;
  16. Button btnshow2;
  17. Button btnshow3;
  18. Button btnshow4;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. btnshow1 = (Button) findViewById(R.id.btnShow);
  25. btnshow1.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. Toast toast = Toast.makeText(MainActivity.this, "测试Toast弹出消息框", Toast.LENGTH_SHORT);
  29. toast.show();
  30. }
  31. });
  32.  
  33. btnshow2 = (Button) findViewById(R.id.btnShow2);
  34. btnshow2.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. Toast toast = Toast.makeText(MainActivity.this, "更改Toast弹出位置", Toast.LENGTH_SHORT);
  38. //第一个参数设置Toast的相对位置,第二个参数为x坐标(正为右,负为左),第三个参数为y坐标(正为下,负为上)
  39. toast.setGravity(Gravity.CENTER, 0, -250);
  40. toast.show();
  41. }
  42. });
  43.  
  44. btnshow3 = (Button) findViewById(R.id.btnShow3);
  45. btnshow3.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. Toast toast = Toast.makeText(MainActivity.this, " 带图片的Toast弹出对话框", Toast.LENGTH_SHORT);
  49. //获取toast的布局方式
  50. LinearLayout toast_layout = (LinearLayout) toast.getView();
  51. ImageView iv = new ImageView(MainActivity.this); //创建图片控件
  52. iv.setImageResource(R.mipmap.ic_launcher); //设置显示图片
  53. // toast_layout.addView(iv); //添加图片控件到toast布局中,默认添加到末尾
  54. toast_layout.addView(iv, 0); //添加图片控件到toast布局中,添加到文字之前
  55. toast.show(); //显示图片
  56. }
  57. });
  58.  
  59. btnshow4 = (Button) findViewById(R.id.btnShow4);
  60. btnshow4.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. //获取inflater对象,用来加载视图
  64. LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
  65. View toast_view = inflater.inflate(R.layout.toast_layout,null);
  66. Toast toast = new Toast(MainActivity.this); //创建一个Toast
  67. toast.setView(toast_view); //设置Toast的视图
  68. toast.show(); //显示
  69. }
  70. });
  71. }
  72. }

自定义Toast视图 toast_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="30dp"
  10. android:gravity="center"
  11. android:text="自定义的Toast对话框" />
  12.  
  13. <ImageView
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:src="@android:drawable/sym_def_app_icon" />
  18.  
  19. <TextView
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:gravity="center"
  23. android:text="显示Toast弹出的内容" />
  24. </LinearLayout>

Android学习(十八)Toast的使用的更多相关文章

  1. Android进阶(十八)AndroidAPP开发问题汇总(二)

    Android进阶(十八)AndroidAPP开发问题汇总(二) 端口被占用解决措施: Android使用SimpleAdapter更新ListView里面的Drawable元素: http://ww ...

  2. android学习十四(android的接收短信)

    收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...

  3. Android学习十二:自定义控件学习

    自定义控件: 1.定义控件的属性,atts.xml 2.代码实现自定义控件的控制 3.引用该控件 首先定义相关的属性 <?xml version="1.0" encoding ...

  4. 强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)

    在强化学习(十七) 基于模型的强化学习与Dyna算法框架中,我们讨论基于模型的强化学习方法的基本思路,以及集合基于模型与不基于模型的强化学习框架Dyna.本文我们讨论另一种非常流行的集合基于模型与不基 ...

  5. android学习十二(android的Content Provider(内容提供器)的使用)

    文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...

  6. Android学习笔记之Toast详解

    1. 贴一段Android API-Toast Toast public class Toast  extends Object java.lang.Object ↳ android.widget.T ...

  7. Android学习十---Android Camera

    Android camera用来拍照和拍摄视频的先看一下最后实现的效果图             最后的效果图 一.准备 在你的应用程序上使用android拍照设备,需要考虑以下几个方面 1. 是否是 ...

  8. Android学习(八) 打开Activity

    在Android中打开窗口有两种方式,第一种是不需要返回值的,第二种是带返回值的. Main.xml文件,程序从这个窗口开始执行. <LinearLayout xmlns:android=&qu ...

  9. Scala学习十八——高级类型

    一.本章要点 单例类型可用于方法串接和带对象参数的方法 类型投影对所有外部类的对象都包含了其他内部类的实例 类型别名给类型指定一个短小的名称 结构类型等效于”鸭子类型“ 存在类型为泛型的通配参数提供了 ...

  10. Android学习十二:跑马灯程序实现(简单联系)

    package org.tonny; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; ...

随机推荐

  1. hihoCoder 1015KMP

    #include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...

  2. HDU3068 最长回文

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  3. [ CodeVS冲杯之路 ] P1166

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1166/ 有许久没有刷题了,忙着过中秋去了嘿嘿 首先它的每一行是独立的,我们可以直接把它拆分成 n 互不相关的子问题做 ...

  4. leetcode-Min Cost Climbing Stairs

    题目: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you ...

  5. springBoot Feign Hystrix

    1.引入依赖包 <!-- 引入关于 hystrix的依赖 --> <dependency> <groupId>org.springframework.cloud&l ...

  6. Visual Studio跨平台开发(3):Xamarin iOS多页面应用开发

    前言 在前一篇教学中, 我们学会如何使用Visual Studio 搭配Xcode进行iOS基本控制项的操作. 但都是属于单一画面的应用程式. 这次我们要来练习如何通过Navigation Contr ...

  7. (7)java基础知识-原码、反码、补码、运算符

    一.原码.反码.补码 原码 一个数转化成二进制. 用最高位来表示正负,最高位为0表示正数,最高位为1表示负数. 例如: short i=5: 因为在java里short占2个字节转化成二进制就是 00 ...

  8. Requirement Analysis

    BRD:Business Requirements Document,商业需求文档.这是产品声明周期中最早的问的文档,再早就应该是脑中的构思了,其内容涉及市场分析,销售策略,盈利预测等,通常是和老大们 ...

  9. 2017 China Collegiate Programming Contest Final (CCPC 2017)

    题解右转队伍wiki https://acm.ecnu.edu.cn/wiki/index.php?title=2017_China_Collegiate_Programming_Contest_Fi ...

  10. 解决android客户端使用soap与服务器通讯错误415

    在编写一个android client与服务器使用soap通讯,虽然能连上但不是正常的200代码,而是415,经查询是"HTTP 415 错误 – 不 支持的媒体类型(Unsupported ...