Android消息提示框Toast
Toast是Android中一种简易的消息提示框。和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,toast会根据用户设置的显示时间后自动消失。
创建Toast的方法总共有2种:
1.Toast.makeText(Context context, (CharSequence text)/( int resId), int duration)
参数:context是指上下文对象,通常是当前的Activity,text是指自己写的消息内容,resId指显示内容引用Resource的那条数据,duration指Toast的显示时间,Toast默认有LENGTH_SHORT和LENGTH_LONG两常量,分别表示时间长和短,也可以自定义时间,如:5000表示显示5秒。用此方法获得Toast对象之后调用.show()方法即可显示消息。
2.自定义Toast,通过Toast toast = new Toast(Context context)获得Toast对象,调用.show()方法即可显示消息。
toast.setDuration(duration)设置显示时间,
toast.setGravity(gravity, xOffset, yOffset)设置显示位置,三个参数分别表示(起点位置,水平位移,垂直位移),
toast.setMargin(float horizontalMargin, float verticalMargin)以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下),
toast.setText(text)设置消息内容,
toast.setView(view)设置Toast显示的视图组件。
 
实例:
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:gravity="center"
  6. android:orientation="vertical"
  7. android:padding="10dp" >
  8. <Button
  9. android:id="@+id/simpleToast"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="默认" >
  13. </Button>
  14. <Button
  15. android:id="@+id/simpleToastWithCustomPosition"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:text="自定义显示位置" >
  19. </Button>
  20. <Button
  21. android:id="@+id/imageToast"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="带图片" >
  25. </Button>
  26. <Button
  27. android:id="@+id/customToast"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:text="完全自定义" >
  31. </Button>
  32. <Button
  33. android:id="@+id/runToastFromOtherThread"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:text="其他线程" >
  37. </Button>
  38. </LinearLayout>

custom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/toastLayout"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_margin="1dp"
  11. android:background="#333399"
  12. android:gravity="center"
  13. android:text="ToastTitle"
  14. android:textColor="#ffffff" />
  15. <LinearLayout
  16. android:id="@+id/toastContent"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginBottom="1dp"
  20. android:layout_marginLeft="1dp"
  21. android:layout_marginRight="1dp"
  22. android:background="#3366ff"
  23. android:orientation="vertical"
  24. android:padding="15dp" >
  25. <ImageView
  26. android:id="@+id/customImageToast"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_gravity="center" />
  30. <TextView
  31. android:id="@+id/customTextToast"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:gravity="center"
  35. android:paddingLeft="10dp"
  36. android:paddingRight="10dp" />
  37. </LinearLayout>
  38. </LinearLayout>

MainActivity.java

  1. public class MainActivity extends Activity implements OnClickListener {
  2. private Button simpleToastBtn, simpleToastWithCustomPositionBtn,
  3. imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;
  4. Handler handler = new Handler();
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. simpleToastBtn = (Button) findViewById(R.id.simpleToast);
  10. simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);
  11. imageToastBtn = (Button) findViewById(R.id.imageToast);
  12. customToastBtn = (Button) findViewById(R.id.customToast);
  13. runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);
  14. simpleToastBtn.setOnClickListener(this);
  15. simpleToastWithCustomPositionBtn.setOnClickListener(this);
  16. imageToastBtn.setOnClickListener(this);
  17. customToastBtn.setOnClickListener(this);
  18. runToastFromOtherThreadBtn.setOnClickListener(this);
  19. }
  20. @Override
  21. public void onClick(View v) {
  22. Toast toast = null;
  23. switch (v.getId()) {
  24. case R.id.simpleToast:
  25. Toast.makeText(MainActivity.this, "默认Toast样式", Toast.LENGTH_LONG)
  26. .show();
  27. break;
  28. case R.id.simpleToastWithCustomPosition:
  29. toast = Toast.makeText(MainActivity.this, "自定义位置Toast",
  30. Toast.LENGTH_LONG);
  31. toast.setGravity(Gravity.CENTER, 0, 0);
  32. toast.show();
  33. break;
  34. case R.id.imageToast:
  35. toast = Toast.makeText(MainActivity.this, "带图片的Toast",
  36. Toast.LENGTH_LONG);
  37. LinearLayout layout = (LinearLayout) toast.getView();
  38. ImageView image = new ImageView(MainActivity.this);
  39. image.setImageResource(R.drawable.ic_launcher);
  40. layout.addView(image, 0);
  41. toast.show();
  42. break;
  43. case R.id.customToast:
  44. LayoutInflater inflater = getLayoutInflater();
  45. View view = inflater.inflate(R.layout.custom, null);
  46. ImageView customImage = (ImageView) view
  47. .findViewById(R.id.customImageToast);
  48. TextView customText = (TextView) view
  49. .findViewById(R.id.customTextToast);
  50. customImage.setImageResource(R.drawable.ic_launcher);
  51. customText.setText("完全自定义Toast");
  52. toast = new Toast(MainActivity.this);
  53. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);
  54. toast.setDuration(Toast.LENGTH_LONG);
  55. toast.setView(view);
  56. toast.show();
  57. break;
  58. case R.id.runToastFromOtherThread:
  59. new Thread(new Runnable() {
  60. public void run() {
  61. showToast();
  62. }
  63. }).start();
  64. break;
  65. }
  66. }
  67. public void showToast() {
  68. handler.post(new Runnable() {
  69. @Override
  70. public void run() {
  71. Toast.makeText(MainActivity.this, "我来自其他线程", Toast.LENGTH_LONG)
  72. .show();
  73. }
  74. });
  75. }
  76. }

Android消息提示框Toast的更多相关文章

  1. 微信小程序之----消息提示框toast

    toast toast为消息提示框,无按钮,如需关闭弹框可以添加事件设置hidden为true,在弹框显示后经过duration指定的时间后触发bindchange绑定的函数. 官方文档 .wxml ...

  2. Android应用开发学习之Toast消息提示框

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1.  创建一个Toast对象.可 ...

  3. Android:Toast简单消息提示框

    Toast是简单的消息提示框,一定时间后自动消失,没有焦点. 1.简单文本提示的方法: Toast.makeText(this, "默认的toast", Toast.LENGTH_ ...

  4. Android学习笔记通过Toast显示消息提示框

    显示消息提示框的步骤 这个很简单我就直接上代码了: Button show = (Button)findViewById(R.id.show); show.setOnClickListener(new ...

  5. Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影

    效果图: 1.activity_main.xml 描述: a.定义了一个消息提示框按钮 点击按钮弹出消息 b.定义了一个选择城市的输入框 点击按钮选择城市 c.定义了一个单选提示框按钮 点击按钮选择某 ...

  6. Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)

    Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...

  7. Flutter Toast消息提示框插件

    Flutter Toast消息提示框插件 在开发flutter项目中,想必大家肯定会用到toast消息提示,说到这里, 大家肯定会想到https://pub.dev/ 插件库, 但是插件市场上有太多类 ...

  8. UWP中的消息提示框(二)

    在UWP中的消息提示框(一)中介绍了一些常见的需要用户主动去干涉的一些消息提示框,接下来打算聊聊不需要用户主动去干涉的一些消息提示框.效果就是像双击退出的那种提示框. 先说说比较简单的吧,通过系统To ...

  9. 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog

    SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...

随机推荐

  1. 安装vb6 正在更新系统 无响应

    新装的win10系统,安装vb6时,最后一直卡在“正在更新系统”,程序无响应,没办法,kill掉后,貌似不影响软件使用,但是安装vs6sp6B无法成功安装. 解决办法是: 不安装“数据访问”组件. 参 ...

  2. PHP中require和include的区别

    include()与require()的功能相同 include(include_once) 与 require(require_once)都是把把包含的文件代码读入到指定位置来,但是二者再用法上有区 ...

  3. HBase数据的导入和导出

    查阅了几篇中英文资料,发现有的地方说的不是很全部,总结在此,共有两种命令行的方式来实现数据的导入导出功能,即备份和还原. 1 HBase本身提供的接口 其调用形式为: 1)导入 ./hbase org ...

  4. nginx(ubuntu)设置别名访问目录

    1 2 3 4 5 6 7 8 9 10 11 12 13 location /phpmyadmin/ { alias  /data/phpmyadmin/; index  index.html in ...

  5. WebService测试方案

    1.WebService简介 WebService是一种革命性的分布式计算技术,本质上就是网络上可用的API,可以直接在网络环境调用的方法. WebService常用的框架有axis.xfire.cx ...

  6. vmware 命令行启动虚拟机

    在redhat enterprise 5.3安装了vmware workstation,如何用vmware 命令行启动指定虚拟机?/usr/bin/vmware[root@node0 ~]# /usr ...

  7. Chapter 2 Open Book——7

    I gunned my deafening engine to life, ignoring the heads that turned inmy direction, and backed care ...

  8. hdu_5806_NanoApe Loves Sequence Ⅱ(双指针)

    题目链接:hdu_5806_NanoApe Loves Sequence Ⅱ 题意: 给你一段数,问你有多少个区间满足第K大的数不小于m 题解: 直接双指针加一下区间就行 #include<cs ...

  9. what a malloc has to do

    1) Allocate a chunk of memory big enough to satisfy the request, and return a pointer to it.2) Remem ...

  10. The project target (Android 6.0) was not properly loaded或者The rendering target (Android 6.0) is still loading.

    第一步:在project上右键选择Properties,然后选择Android, 然后在Project Build Target那里选择其他的Target试试: 第二步: 我选了6.0之后,就有菜单可 ...