1. 简介:toast是“吐司”的意思,它属于android杂项组件,是一个简单的消息提示框,类似于javascript中的alert.
  2. 作用
  • 显示文本
  • 显示图片
  • 显示图文

3.常用方法

show():显示消息提示框

makeText(Context context,Charsquence text,int duration):创建一个toast对象

setView(View view):该Toast显示的view组件

4.代码演示

MainActivity.class
public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void findOne(View v){
Toast.makeText(this,"显示文本",Toast.LENGTH_LONG).show();
}
public void findTwo(View v){
Toast toast = new Toast(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
toast.setView(imageView);
toast.setGravity(Gravity.TOP,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
public void findThree(View v){
Toast toast = new Toast(this); TextView tv = new TextView(this);
tv.setText("安卓开机图片"); ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher); LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView);
layout.addView(tv);
toast.setView(layout); toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
} }

  

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.langdon.taiyang.androidtest.MainActivity">
<Button
android:text="toast显示文本"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toidOne"
android:onClick="findOne"
android:layout_gravity="center_horizontal"
/>
<Button
android:text="toast显示图片"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toidTwo"
android:onClick="findTwo"
android:layout_gravity="center_horizontal"/>
<Button
android:text="toast显示图文"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toidThree"
android:onClick="findThree"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

  

5.注意点

toast组件小结的更多相关文章

  1. 提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现

    在java下org.socrates.mydiary.activity下LoginActivity下自定义一个方法showCustomerToast()  public class LoginAct ...

  2. 微信小程序把玩(二十四)toast组件

    原文:微信小程序把玩(二十四)toast组件 toast消息提示框,可用在提示一些信息,比如清楚缓存给用户一个友好的提示!或操作一些请求不想让用户有什么操作,toast也可以做到因为toast显示时其 ...

  3. 百度小程序自定义通用toast组件

    百度小程序Toast组件 author: @TiffanysBear 百度小程序自定义通用toast组件 BdToast百度小程序自定义通用组件-github地址 需求 手百小程序的toast仅支持在 ...

  4. 使用Angular CDK实现一个Service弹出Toast组件

    在Angular中,官方团队在开发Material组件库的同时,顺手做了一套Component dev kit,也就是在Angular世界中大名鼎鼎的CDK,这套工具包提供了非常多的前端开发的通用功能 ...

  5. 提示框的优化之自定义Toast组件之(三)Toast组件优化

    开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...

  6. 提示框的优化之自定义Toast组件之(一)Toast组件的布局实现

    开发步骤:  在res下layout下创建一个Toast的布局资源文件toast_customer.xml  在最外层布局组件中为该布局添加android:id属性  //toast_custo ...

  7. 每日技术总结:Toast组件,eslint,white-space,animate,$emit

    1.一个优雅的提示是网站必不可少的. 请参考:vue2.0 自定义 提示框(Toast)组件 2.ESLint使用总结 (1)在.eslintrc.js里关闭某条规则, '规则名': 'off'或0 ...

  8. 小程序中怎么引入wepy.js第三方toast组件

    1.先安装组件 npm install wepy-com-toast --save 2.引入wepy和toast import wepy from 'wepy' import Toast from ' ...

  9. vue中的toast组件

    首先在components新建组件文件夹 随后在toast.vue中写入弹框样式 <template> <transition name="demo"> & ...

随机推荐

  1. ACM模板(持续补完)

    1.KMP #include<cstring> #include<algorithm> #include<cstdio> using namespace std; ...

  2. 安装 pyopenssl c/_cffi_backend.c:15:17: 致命错误:ffi.h:

    错误 c/_cffi_backend.c:15:17: 致命错误:ffi.h: 解决方案 yum install -y libffi-devel 或ubuntu中 apt-get install -y ...

  3. jQuery遍历checkbox

    $("input[type='checkbox']").each(function(){ var value = $(this).val(); //获得值 $(this).attr ...

  4. 一次kubernetes资源文件创建失败的排查

    今天在jenkins中创建kubernetes的rc时,检查目标yaml文件时报出如下错误: + /opt/jenkins/kube/kubectl -s http://10.xx.xx.xx:808 ...

  5. Atomikos的使用过程中要注意的事

    在使用Atomikos过程中遇到的一些问题,以作记录: MySQL does not support TMJOIN MySQL does not allow for joining an existi ...

  6. Javascript最简单的模板引擎

    非常简单,性能估计一般,方法最原始. //第一代模板引擎 //只支持{{key}}的替换,不支持语句 //支持Object和Array对象 function template_setdata(temp ...

  7. Web应用请求和响应 HTTP相关

    (1)请求:浏览器以HTTP协议的方式提交请求到服务器 (2)响应:服务器以HTTP协议的方式响应内容到浏览器 注意:HTTP是WEB大众化非安全协议 HTTPS是WEB安全协议,是基于HTTP协议的 ...

  8. 【从html到算法框架】科技白学习计划书

    一.学习计划 根据对Web知识层次的梳理,我觉得可以把它分别以技术和思想两个维度进行分类.从技术维度上讲,Web知识有内容.结构.样式.操作(静态的修改与动态的修改(动态,例如使用定时器)).交互(与 ...

  9. Mui.ajax请求服务器正确返回json数据格式

    ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...

  10. POJ1390Blocks(DP+好题+抽空再来理解理解)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4744   Accepted: 1930 Descriptio ...