Android学习(十八)Toast的使用
一、什么是Toast
1、Toast是一种提供给用户简洁提示信息的视图。
2、该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示。
3、Android提供的Toast类可以创建和显示Toast信息。
下面演示4种Toast的用法,普通的 Toast,更改位置的Toast,显示图片的Toast,自定义的Toast分别使用4个按钮来实现。
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"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/btnShow"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Toast显示"/>
- <Button
- android:id="@+id/btnShow2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Toast显示位置"/>
- <Button
- android:id="@+id/btnShow3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Toast显示图片"/>
- <Button
- android:id="@+id/btnShow4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="自定义Toast显示layout文件"/>
- </LinearLayout>
main.java
- package com.example.zhengcheng.myapp;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- Button btnshow1;
- Button btnshow2;
- Button btnshow3;
- Button btnshow4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnshow1 = (Button) findViewById(R.id.btnShow);
- btnshow1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast toast = Toast.makeText(MainActivity.this, "测试Toast弹出消息框", Toast.LENGTH_SHORT);
- toast.show();
- }
- });
- btnshow2 = (Button) findViewById(R.id.btnShow2);
- btnshow2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast toast = Toast.makeText(MainActivity.this, "更改Toast弹出位置", Toast.LENGTH_SHORT);
- //第一个参数设置Toast的相对位置,第二个参数为x坐标(正为右,负为左),第三个参数为y坐标(正为下,负为上)
- toast.setGravity(Gravity.CENTER, 0, -250);
- toast.show();
- }
- });
- btnshow3 = (Button) findViewById(R.id.btnShow3);
- btnshow3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast toast = Toast.makeText(MainActivity.this, " 带图片的Toast弹出对话框", Toast.LENGTH_SHORT);
- //获取toast的布局方式
- LinearLayout toast_layout = (LinearLayout) toast.getView();
- ImageView iv = new ImageView(MainActivity.this); //创建图片控件
- iv.setImageResource(R.mipmap.ic_launcher); //设置显示图片
- // toast_layout.addView(iv); //添加图片控件到toast布局中,默认添加到末尾
- toast_layout.addView(iv, 0); //添加图片控件到toast布局中,添加到文字之前
- toast.show(); //显示图片
- }
- });
- btnshow4 = (Button) findViewById(R.id.btnShow4);
- btnshow4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //获取inflater对象,用来加载视图
- LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
- View toast_view = inflater.inflate(R.layout.toast_layout,null);
- Toast toast = new Toast(MainActivity.this); //创建一个Toast
- toast.setView(toast_view); //设置Toast的视图
- toast.show(); //显示
- }
- });
- }
- }
自定义Toast视图 toast_layout.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:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:gravity="center"
- android:text="自定义的Toast对话框" />
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@android:drawable/sym_def_app_icon" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="显示Toast弹出的内容" />
- </LinearLayout>
Android学习(十八)Toast的使用的更多相关文章
- Android进阶(十八)AndroidAPP开发问题汇总(二)
Android进阶(十八)AndroidAPP开发问题汇总(二) 端口被占用解决措施: Android使用SimpleAdapter更新ListView里面的Drawable元素: http://ww ...
- android学习十四(android的接收短信)
收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...
- Android学习十二:自定义控件学习
自定义控件: 1.定义控件的属性,atts.xml 2.代码实现自定义控件的控制 3.引用该控件 首先定义相关的属性 <?xml version="1.0" encoding ...
- 强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)
在强化学习(十七) 基于模型的强化学习与Dyna算法框架中,我们讨论基于模型的强化学习方法的基本思路,以及集合基于模型与不基于模型的强化学习框架Dyna.本文我们讨论另一种非常流行的集合基于模型与不基 ...
- android学习十二(android的Content Provider(内容提供器)的使用)
文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...
- Android学习笔记之Toast详解
1. 贴一段Android API-Toast Toast public class Toast extends Object java.lang.Object ↳ android.widget.T ...
- Android学习十---Android Camera
Android camera用来拍照和拍摄视频的先看一下最后实现的效果图 最后的效果图 一.准备 在你的应用程序上使用android拍照设备,需要考虑以下几个方面 1. 是否是 ...
- Android学习(八) 打开Activity
在Android中打开窗口有两种方式,第一种是不需要返回值的,第二种是带返回值的. Main.xml文件,程序从这个窗口开始执行. <LinearLayout xmlns:android=&qu ...
- Scala学习十八——高级类型
一.本章要点 单例类型可用于方法串接和带对象参数的方法 类型投影对所有外部类的对象都包含了其他内部类的实例 类型别名给类型指定一个短小的名称 结构类型等效于”鸭子类型“ 存在类型为泛型的通配参数提供了 ...
- Android学习十二:跑马灯程序实现(简单联系)
package org.tonny; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; ...
随机推荐
- hihoCoder 1015KMP
#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...
- HDU3068 最长回文
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- [ CodeVS冲杯之路 ] P1166
不充钱,你怎么AC? 题目:http://codevs.cn/problem/1166/ 有许久没有刷题了,忙着过中秋去了嘿嘿 首先它的每一行是独立的,我们可以直接把它拆分成 n 互不相关的子问题做 ...
- leetcode-Min Cost Climbing Stairs
题目: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you ...
- springBoot Feign Hystrix
1.引入依赖包 <!-- 引入关于 hystrix的依赖 --> <dependency> <groupId>org.springframework.cloud&l ...
- Visual Studio跨平台开发(3):Xamarin iOS多页面应用开发
前言 在前一篇教学中, 我们学会如何使用Visual Studio 搭配Xcode进行iOS基本控制项的操作. 但都是属于单一画面的应用程式. 这次我们要来练习如何通过Navigation Contr ...
- (7)java基础知识-原码、反码、补码、运算符
一.原码.反码.补码 原码 一个数转化成二进制. 用最高位来表示正负,最高位为0表示正数,最高位为1表示负数. 例如: short i=5: 因为在java里short占2个字节转化成二进制就是 00 ...
- Requirement Analysis
BRD:Business Requirements Document,商业需求文档.这是产品声明周期中最早的问的文档,再早就应该是脑中的构思了,其内容涉及市场分析,销售策略,盈利预测等,通常是和老大们 ...
- 2017 China Collegiate Programming Contest Final (CCPC 2017)
题解右转队伍wiki https://acm.ecnu.edu.cn/wiki/index.php?title=2017_China_Collegiate_Programming_Contest_Fi ...
- 解决android客户端使用soap与服务器通讯错误415
在编写一个android client与服务器使用soap通讯,虽然能连上但不是正常的200代码,而是415,经查询是"HTTP 415 错误 – 不 支持的媒体类型(Unsupported ...