Android消息提示框Toast
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical"
- android:padding="10dp" >
- <Button
- android:id="@+id/simpleToast"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="默认" >
- </Button>
- <Button
- android:id="@+id/simpleToastWithCustomPosition"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="自定义显示位置" >
- </Button>
- <Button
- android:id="@+id/imageToast"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="带图片" >
- </Button>
- <Button
- android:id="@+id/customToast"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="完全自定义" >
- </Button>
- <Button
- android:id="@+id/runToastFromOtherThread"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="其他线程" >
- </Button>
- </LinearLayout>
custom.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toastLayout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="1dp"
- android:background="#333399"
- android:gravity="center"
- android:text="ToastTitle"
- android:textColor="#ffffff" />
- <LinearLayout
- android:id="@+id/toastContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="1dp"
- android:layout_marginLeft="1dp"
- android:layout_marginRight="1dp"
- android:background="#3366ff"
- android:orientation="vertical"
- android:padding="15dp" >
- <ImageView
- android:id="@+id/customImageToast"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center" />
- <TextView
- android:id="@+id/customTextToast"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:paddingLeft="10dp"
- android:paddingRight="10dp" />
- </LinearLayout>
- </LinearLayout>
MainActivity.java
- public class MainActivity extends Activity implements OnClickListener {
- private Button simpleToastBtn, simpleToastWithCustomPositionBtn,
- imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;
- Handler handler = new Handler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- simpleToastBtn = (Button) findViewById(R.id.simpleToast);
- simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);
- imageToastBtn = (Button) findViewById(R.id.imageToast);
- customToastBtn = (Button) findViewById(R.id.customToast);
- runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);
- simpleToastBtn.setOnClickListener(this);
- simpleToastWithCustomPositionBtn.setOnClickListener(this);
- imageToastBtn.setOnClickListener(this);
- customToastBtn.setOnClickListener(this);
- runToastFromOtherThreadBtn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Toast toast = null;
- switch (v.getId()) {
- case R.id.simpleToast:
- Toast.makeText(MainActivity.this, "默认Toast样式", Toast.LENGTH_LONG)
- .show();
- break;
- case R.id.simpleToastWithCustomPosition:
- toast = Toast.makeText(MainActivity.this, "自定义位置Toast",
- Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- break;
- case R.id.imageToast:
- toast = Toast.makeText(MainActivity.this, "带图片的Toast",
- Toast.LENGTH_LONG);
- LinearLayout layout = (LinearLayout) toast.getView();
- ImageView image = new ImageView(MainActivity.this);
- image.setImageResource(R.drawable.ic_launcher);
- layout.addView(image, 0);
- toast.show();
- break;
- case R.id.customToast:
- LayoutInflater inflater = getLayoutInflater();
- View view = inflater.inflate(R.layout.custom, null);
- ImageView customImage = (ImageView) view
- .findViewById(R.id.customImageToast);
- TextView customText = (TextView) view
- .findViewById(R.id.customTextToast);
- customImage.setImageResource(R.drawable.ic_launcher);
- customText.setText("完全自定义Toast");
- toast = new Toast(MainActivity.this);
- toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(view);
- toast.show();
- break;
- case R.id.runToastFromOtherThread:
- new Thread(new Runnable() {
- public void run() {
- showToast();
- }
- }).start();
- break;
- }
- }
- public void showToast() {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(MainActivity.this, "我来自其他线程", Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- }
Android消息提示框Toast的更多相关文章
- 微信小程序之----消息提示框toast
toast toast为消息提示框,无按钮,如需关闭弹框可以添加事件设置hidden为true,在弹框显示后经过duration指定的时间后触发bindchange绑定的函数. 官方文档 .wxml ...
- Android应用开发学习之Toast消息提示框
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1. 创建一个Toast对象.可 ...
- Android:Toast简单消息提示框
Toast是简单的消息提示框,一定时间后自动消失,没有焦点. 1.简单文本提示的方法: Toast.makeText(this, "默认的toast", Toast.LENGTH_ ...
- Android学习笔记通过Toast显示消息提示框
显示消息提示框的步骤 这个很简单我就直接上代码了: Button show = (Button)findViewById(R.id.show); show.setOnClickListener(new ...
- Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影
效果图: 1.activity_main.xml 描述: a.定义了一个消息提示框按钮 点击按钮弹出消息 b.定义了一个选择城市的输入框 点击按钮选择城市 c.定义了一个单选提示框按钮 点击按钮选择某 ...
- Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)
Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...
- Flutter Toast消息提示框插件
Flutter Toast消息提示框插件 在开发flutter项目中,想必大家肯定会用到toast消息提示,说到这里, 大家肯定会想到https://pub.dev/ 插件库, 但是插件市场上有太多类 ...
- UWP中的消息提示框(二)
在UWP中的消息提示框(一)中介绍了一些常见的需要用户主动去干涉的一些消息提示框,接下来打算聊聊不需要用户主动去干涉的一些消息提示框.效果就是像双击退出的那种提示框. 先说说比较简单的吧,通过系统To ...
- 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog
SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...
随机推荐
- iOS解析后台返回的二进制图片
UIEdgeInsets insets = {0,0,0,0}; [self.showidentifyButton setImageEdgeInsets:insets]; NSData *imageD ...
- 第二部分 职责型模式responsibility
普通职责无法提供的内容,据此可以定义以下几种模式: 将职责集中到某个类的一个单独实例,单件模式 当一个对象发生改变时,依赖于这个对象的其他对象都能够得到通知,而这个发生改变的对象无须了解自己被其他哪些 ...
- ACE_Message_Block消息数据类
ACE_Message_Block ACE_Message_Block用于构建"固定"和"可变"长度的消息.ACE_Message_Block可以将多条消息连接 ...
- Controller <-> View 一般视图层级
关于 self.navigationController.view 相信看过 MBProgressHUD 官方例子 HudDemo 代码的同学应该看到过下述代码: 1 HUD = [[MBProgre ...
- websphere安装验证报错 忘记密码的配置
http://blog.csdn.net/yulimin/article/details/4048897 ADMU7704E: 在尝试启动与服务器相关联的 Windows 服务时失败:server1: ...
- OC工程调用Swift方法
1.建一个OC工程命名为SwiftOC.如图所示: 2.新建一个swfit文件命名为Test.swift,会弹出提示,选择Create Bridging Header建立桥接文件,系统会建立“工程名- ...
- JavaScript在智能手机上的应用-判断是否为移动浏览器
-------------------- <script type="text/javascript"> var userAgent = navi ...
- lldpcli 常用命令
1.查看周围的邻居列表 root@hbg:/# lldpcli show neighbors------------------------------------------------------ ...
- C# WebBrowser函数互相调用
在使用C#开发winform程序过程中,我们经常会碰到嵌入了一个WebBrowser的浏览器控件.很多时候,我们需要在程序里控制网页的显示方式,或者调用网页当中的某个JS函数,反过来,也有可能网页也需 ...
- 几种访问其他域swf文件,或本地浏览器运行环境【安全沙箱】冲突解决方法
声明: 几种方法都源自网络,我只负责汇总一下子,因为来自多方转载,也找不到初始出处了,需要的可以随意收藏,物尽其用,蛮好^_^ 对于类似的出错信息:SecurityError: Error #2148 ...