2019独角兽企业重金招聘Python工程师标准>>>

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。

1.默认效果

代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();

2.自定义显示位置效果

代码

toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();

3.带图片效果

代码

toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();

4.完全自定义效果

代码

LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();

5.其他线程

代码

new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();

完整代码

1.Main,java

package com.wjq.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
 Handler handler = new Handler();

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
    this);
  findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  findViewById(R.id.btnCustomToast).setOnClickListener(this);
  findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

}

public void showToast() {
  handler.post(new Runnable() {

@Override
   public void run() {
    Toast.makeText(getApplicationContext(), "我来自其他线程!",
      Toast.LENGTH_SHORT).show();

}
  });
 }

@Override
 public void onClick(View v) {
  Toast toast = null;
  switch (v.getId()) {
  case R.id.btnSimpleToast:
   Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();
   break;
  case R.id.btnSimpleToastWithCustomPosition:
   toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();
   break;
  case R.id.btnSimpleToastWithImage:
   toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();
   break;
  case R.id.btnCustomToast:
   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
   break;
  case R.id.btnRunToastFromOtherThread:
   new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();
   break;

}

}
}

2.main,xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  android:text="默认"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="自定义显示位置"
  android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  android:text="带图片"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="完全自定义"
  android:id="@+id/btnCustomToast"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="其他线程"
  android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>

3.custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:layout_width="wrap_content"
 android:background="#ffffffff" android:orientation="vertical"
 android:id="@+id/llToast" >
 <TextView
  android:layout_height="wrap_content"
  android:layout_margin="1dip"
  android:textColor="#ffffffff"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:background="#bb000000"
  android:id="@+id/tvTitleToast" />
 <LinearLayout
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/llToastContent"
  android:layout_marginLeft="1dip"
  android:layout_marginRight="1dip"
  android:layout_marginBottom="1dip"
  android:layout_width="wrap_content"
  android:padding="15dip"
  android:background="#44000000" >
  <ImageView
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:id="@+id/tvImageToast" />
  <TextView
   android:layout_height="wrap_content"
   android:paddingRight="10dip"
   android:paddingLeft="10dip"
   android:layout_width="wrap_content"
   android:gravity="center"
   android:textColor="#ff000000"
   android:id="@+id/tvTextToast" />
 </LinearLayout>
</LinearLayout>

转载于:https://my.oschina.net/xxjbs001/blog/372331

Android中Toast的用法简介相关推荐

  1. java toast_Android中Toast的用法简介

    Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.下面用一个实例来看看如何使用Toast ...

  2. Android中Parcelable接口用法

    --  通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象.也可以将Parcel看成是一个流,通过writeToPa ...

  3. php中__FILE__常量用法简介

    php中__FILE__常量用法简介 http://blog.csdn.net/xbei07/article/details/5616020#comments出处 1.php中的__FILE__常量返 ...

  4. android 类型.this,Android中this的用法

    关于Android中this的用法解释 问题由来 由于很多同学在学习Android时候没有对Java有很深的了解,很多人都会对代码中各种各样的this产生疑惑. 以<第一行代码Android&g ...

  5. Matlab中 intlinprog函数用法简介

    Matlab中 intlinprog函数用法简介 本来想要自己亲手写一遍的,发现了一优质博文基本上跟我做过的例题大差不差,所以就直接放上链接. 参考链接 https://www.cnblogs.com ...

  6. Android中am命令用法详解

    Android中am命令用法 位于frameworks/base/cmds/pm am命令作用:管理Activity usage: am [start|broadcast|instrument|pro ...

  7. Android中Parcelable接口用法 和 Serializable实现与Parcelabel实现的区别

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  8. Android中对Handler用法的总结

    Android之Handler用法总结 方法一:(java习惯,在android平台开发时这样是不行的,因为它违背了单线程模型) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用 ...

  9. android toast 一直显示,android中toast无法显示问题

    本文记录两种情况下Android的toast无法显示的问题及提供对应的解决方案 关闭通知权限 在Android系统中将通知栏权限,我们可以发现toast无法展示出来(绝大部分机子,oppo和vivo一 ...

最新文章

  1. 如何禁止浏览器自动填充
  2. iOS 开发 OC编程 数组冒泡排序.图书管理
  3. 浅谈tidb事务与MySQL事务之间的区别
  4. 局域网配置NTP服务器
  5. 内存泄漏_内存泄漏–测量频率和严重性
  6. 项目托管到Github
  7. C++类与new和delete操作符
  8. mysql中删除标识列的语句_MySQL中一些常用的数据表操作语句笔记
  9. 智伴机器人课文跟读哪里有_仁爱版英语九年级上册Unit4 Topic1单词课文朗读/翻译+知识汇总...
  10. mysql初始化错误【一】Can't find error-message file '/usr/local/mysql/errmsg.sys'
  11. Foxmail 绑定企业邮箱
  12. 服务器上不安装anaconda而拥有两个环境
  13. 图谱论(Spectral Graph Theory)基础
  14. Linux下 PHP 安装ioncube扩展
  15. 在C#中列出进程,杀掉进程。
  16. 快速搜索Wox工具之Everything Service没有运行报错,解决办法!
  17. 微软「警告」员工不要在愚人节搞事情,为什么?
  18. 质量管理用因果分析计算机成绩差,项目质量管理中的因果分析图
  19. android 802.1x 认证,802.1X认证基础
  20. 华为服务器新版bios修改IPMI,华为服务器ipmi设置

热门文章

  1. SharePoint 【架构系列】-- SharePoint的处理(Process)与执行模型(Trust Model) 01
  2. RUP概述与实际应用的例子
  3. java学习笔记(十二)----集合
  4. mqtt连接失败_Netty实战:如何让单机下Netty支持百万长连接?
  5. hdu4845 状态压缩BFS
  6. 栈 - 关于出栈序列,判断合法的出栈序列
  7. 【C 语言】字符串模型 ( 字符串翻转模型 )
  8. 【C 语言】字符串操作 ( 使用 数组下标 操作字符串 | 使用 char * 指针 操作字符串 )
  9. 【Android 安全】DEX 加密 ( Java 工具开发 | apk 文件对齐 )
  10. tree树的展示,check树的展示,json数据转zree树格式数据