在前面的实例中,已经应用过Toast类来显示一个简单的提示框了。这次将对Toast进行详细介绍。Toast类用于在屏幕中显示一个消息提示框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一段时间后自动消失。通常用于显示一些快速提示信息,应用范围非常广泛。
使用Toast来显示消息提示框非常简单,只需要一下三个步骤:
(1).创建一个Toast对象。通常有两种方法:一种是使用构造方式进行创建;
Toast toast=new Toast(this);
另一种是调用Toast类的makeText()方法创建。
Toast toast=Toast.makeText(this,"要显示的内容",Toast.LENGTH_SHORT);
(2).调用Toast类提供的方法来设置该消息提示框的对齐方式、页边距、显示的内容等等。
常用的方法如下:
setDuration(int duration) 用于设置消息提示框持续的时间,参数通常使用Toast.LENGTH_LONG或Toast.LENGTH_SHORT
setGravity(int gravity,int xOffset,int yOffset) 用于设置消息提示框的位置,参数grivaty用于指定对齐方式:xOffset和yOffset用于指定具体的偏移值
setMargin(float horizontalMargin,float verticalMargin) 用于设置消息提示的页边距
setText(CharSequence s) 用于设置要显示的文本内容
setView(View view) 用于设置将要在提示框中显示的视图
(3).调用Toast类的show()方法显示消息提示框。需要注意的是,一定要调用该方法,否则设置的消息提示框将不显示。
下面通过一个具体的实例来说明如何使用Toast类显示消息提示框。
res/layout/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:id="@+id/layout1" android:gravity="center_horizontal" > </LinearLayout>
MainActivity:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过makeText方法创建消息提示框 Toast.makeText(MainActivity.this, "我是通过makeText方法创建的消息提示框", Toast.LENGTH_SHORT).show(); //通过Toast类的构造方法创建消息提示框 Toast toast=new Toast(this); toast.setDuration(Toast.LENGTH_SHORT);//设置持续时间 toast.setGravity(Gravity.CENTER,0, 0);//设置对齐方式 LinearLayout ll=new LinearLayout(this);//创建一个线性布局管理器 ImageView imageView=new ImageView(this); imageView.setImageResource(R.drawable.stop); imageView.setPadding(0, 0, 5, 0); ll.addView(imageView); TextView tv=new TextView(this); tv.setText("我是通过构造方法创建的消息提示框"); ll.addView(tv); toast.setView(ll);//设置消息提示框中要显示的视图 toast.show();//显示消息提示框 } }
效果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
Toast 消息提示 基本使用 以下为一个模拟登录成功后,弹出toast提示,并在一定时间(默认2000ms)后,自动跳转页面到个人中心页(也可以配置跳转的参数)的示例 <template> <view> <u-toast ref="uToast" /> </view> </template> <script> export default { methods: { show
本文向大家介绍flutter Toast实现消息提示框,包括了flutter Toast实现消息提示框的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了flutter Toast实现消息提示框的具体代码,供大家参考,具体内容如下 使用方法 Toast 源码 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
main.js import Vue from "vue"; import { Toast } from "feui"; window.Toast = Toast; methods: { showSuccess() { Toast.success({ duration: 1500, message: "操作成功" }); }, sh
toast 用于临时显示某些信息,并且会在数秒后自动消失。这里使用小程序原生 API wx.showToast(OBJECT)来完成。不过有个哥们自己写了一个基于 mpvue框架的一个toast组件,详情请点 mpvue-toast。在mpvue框架中使用wx.showToast(OBJECT)实现如下: <template> <div class="page"> <div class
定义 toast提示组件。 图片展示 代码演示 import Dialog from 'pile/dist/components/dialog' const {Toast} = Dialog <Toast content='3秒后消失' toastShow={true} type='success' // 类型:失败:fail,成功:success,错误:warning,loadi
Since 8.0 toast 显示一个弱提示,可选择多少秒之后消失 使用方法 AlipayJSBridge.call('toast', { content: '操作成功', type: 'success', duration: 2000 }, function() { alert("toast消失后执行"); }); // 可以通过hideToast接口隐藏已经弹出的toast