第6章(2) Toast(信息提示框)

分类:C#、Android、VS2015;

创建日期:2016-02-08

一、简介

Toast用于向用户显示一些帮助或者提示信息。前面我们已经多次用到它,这里只是系统地将其总结一下,并演示它的各种基本用法。

二、示例-- Demo01Toast

1、运行截图

2、添加Demo01_CustomToast.axml文件

在layout文件夹下添加该文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffffff"
    android:id="@+id/custom">
    <TextView
        android:layout_height="wrap_content"
        android:layout_margin="1dip"
        android:textColor="#ffffffff"
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="#16ccdd"
        android:id="@+id/title" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ccffff"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginBottom="1dip"
        android:padding="15dip"
        android:id="@+id/customToastContent">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/picture" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:gravity="center"
            android:textColor="#ff000000"
            android:id="@+id/prompt" />
    </LinearLayout>
</LinearLayout>

3、添加Demo01_Toast.axml文件

在layout文件夹下添加该文件。

<?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:gravity="center">
    <Button
        android:id="@+id/btnDefault"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="默认的位置和样式" />
    <Button
        android:id="@+id/btnPhoto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="带图片的样式(默认位置)" />
    <Button
        android:id="@+id/btnPosition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="指定显示位置的默认样式" />
    <Button
        android:id="@+id/btnCustom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自定义样式和位置" />
    <Button
        android:id="@+id/btnThread"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="来自其他线程(默认位置)" />
</LinearLayout>

4、添加Demo01Toast.cs

在SrcActivity文件夹下添加该文件。

using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;

namespace ch06demos.SrcActivity
{
    [Activity(Label = "Demo01Toast")]
    public class Demo01Toast : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Demo01_Toast);
            var btnDefault = FindViewById<Button>(Resource.Id.btnDefault);
            var btnPhoto = FindViewById<Button>(Resource.Id.btnPhoto);
            var btnPosition = FindViewById<Button>(Resource.Id.btnPosition);
            var btnCustom = FindViewById<Button>(Resource.Id.btnCustom);
            var btnThread = FindViewById<Button>(Resource.Id.btnThread);
            btnDefault.Click += Button_Click;
            btnPhoto.Click += Button_Click;
            btnPosition.Click += Button_Click;
            btnCustom.Click += Button_Click;
            btnThread.Click += Button_Click;
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            switch (btn.Id)
            {
                case Resource.Id.btnDefault:
                    Toast.MakeText(this, "这是默认的样式", ToastLength.Short).Show();
                    break;
                case Resource.Id.btnPhoto:
                    {
                        var toast = Toast.MakeText(this, "这是带图片的Toast样式(默认位置)", ToastLength.Short);
                        LinearLayout toastView = (LinearLayout)toast.View;
                        ImageView imageCodeProject = new ImageView(this);
                        imageCodeProject.SetImageResource(Resource.Drawable.Icon);
                        toastView.AddView(imageCodeProject, 0);
                        toast.Show();
                    }
                    break;
                case Resource.Id.btnPosition:
                    {
                        var toast = Toast.MakeText(this, "这是自定义位置的提示信息", ToastLength.Long);
                        //从中心位置向上偏移300
                        toast.SetGravity(GravityFlags.Center, 0, -300);
                        toast.Show();
                    }
                    break;
                case Resource.Id.btnCustom:
                    {
                        var toast = new Toast(this);
                        toast.View = GetCustomView("这是自定义样式的标题",
                            "这是自定义样式的提示信息", Resource.Drawable.Icon);
                        //从中心位置向上偏移300
                        toast.SetGravity(GravityFlags.Center, 0, -300);
                        toast.Duration = ToastLength.Long;
                        toast.Show();
                    }
                    break;
                case Resource.Id.btnThread:
                    //建议的办法:
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, "这是来自其他线程的提示信息!", ToastLength.Long).Show();
                    });
                    //也可以用下面的办法实现(用Handler实现后台线程与UI线程的交互):
                    //var h = new Handler();
                    //h.Post(() =>
                    //{
                    //    Toast.MakeText(this, "这是来自其他线程的提示信息!", ToastLength.Long).Show();
                    //});
                    break;
            }
        }

        /// <summary>
        /// 获取用Toast显示的自定义视图
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="prompt">提示信息</param>
        /// <param name="pictureId">图片资源的ID</param>
        /// <returns>自定义的视图</returns>
        private View GetCustomView(string title, string prompt, int pictureId)
        {
            //用指定的XML资源文件填充视图的层次结构
            View customView = this.LayoutInflater.Inflate(
                Resource.Layout.Demo01_CustomToast,
                FindViewById<ViewGroup>(Resource.Id.custom));
            //设置标题
            var textViewTitle = customView.FindViewById<TextView>(Resource.Id.title);
            textViewTitle.Text = title;
            //设置显示的图像
            var picture = (ImageView)customView.FindViewById(Resource.Id.picture);
            picture.SetImageResource(pictureId);
            //设置显示的文本内容
            TextView textViewPromet = customView.FindViewById<TextView>(Resource.Id.prompt);
            textViewPromet.Text = prompt;
            return customView;
        }
    }
}

5、运行

按<F5>键调试运行。

时间: 2024-08-01 22:45:34

第6章(2) Toast(信息提示框)的相关文章

高级控件【安卓5】——信息提示框、对话框

Toast信息提示框 1 Button bt1=(Button)findViewById(R.id.Tbt01); 2 Button bt2=(Button)findViewById(R.id.Tbt02); 3 bt1.setOnClickListener(new OnClickListener() { 4 public void onClick(View v) { 5 Toast.makeText(Toast0.this, "按钮1短提示", 6 Toast.LENGTH_SHOR

信息提示框、对话框

Toast信息提示框 1Button bt1=(Button)findViewById(R.id.Tbt01); 2 Button bt2=(Button)findViewById(R.id.Tbt02); 3 bt1.setOnClickListener(new OnClickListener() { 4 public void onClick(View v) { 5 Toast.makeText(Toast0.this, "按钮1短提示", 6 Toast.LENGTH_SHORT

信息提示框

Android-Toast(信息提示框) <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_paren

[ExtJs5.1.0系列-第二天] 信息提示框组件&lt;Ext.MessageBox&gt;

在介绍ExtJs信息提示框组件之前,我们先来介绍一下ExtJs的组件配置. ExtJs组件通常有两种配置形式: (1) 用逗号分割的参数列表;  (2) 使用JSON对象作为组件提供配置信息.对于比较简单的配置一般采用逗号分隔的参数列表进行设置,对于较复杂的配置一般采用JSON对象的方式为组件提供配置信息. JSON简单介绍: JSON的全称是JavaScript Object Notation(JavaScript对象符号).JSON是一种结构化的,轻量级的,完全独立于语言的,基于文本的数据传

强大的响应式jQuery消息通知框和信息提示框插件

lobibox是一款功能非常强大的jQuery消息通知框和信息提示框插件.这个插件分为两个部分:消息通知框和信息提示框.它能非常好的结合Bootstrap使用. 信息提示框 lobibox的信息提示框可以是模态窗口,也可以不是模态窗口. 允许显示多条信息. 允许使用任何可用的CSS动画来显示和隐藏信息提示框. 信息提示框可以显示不同的颜色和图标. 可以设置为确认信息提示框. 可以设置为一行的prompt信息框.(可以使用任何HTML5的输入框类型来作为prompt) 也可以设置为多重prompt

Msgbox信息提示框

在程序运行过程中,常常需要弹出一些提示信息,与用户交流.需采用强制方式,将焦点移动到提示框上来,待用户处理完预先设置的对话之后,才能接着后面的操作. 这些提示框的使用都是本着协助用户更准确,更有效地与计算机交互的意义而设置的,是一个贴心的使用助手.一个有力的助手,就要能在繁杂处给予清晰的指引,在危险处提出警示,在关心的问题上及时给予回馈,在选择问题上要将不同选择的厉害关系讲明了,这样才能成为好助手.既然是协助,就又不能老在眼前晃,要在适当的时候适时地出现,出现得太多,影响效率,反而帮了倒忙.因为

asp.net 右下角弹出新提醒信息提示框

asp.net 开发信息系统,无论是OA或者是其它业务系统中,都会用到新信息提醒功能,特此将方案跟大家分享一下.先上一图给大家看一看: 优点:1).该新消息弹出提示框样式算是比较不错的,个人认为. 2).弹出框内的信息内容.提醒的个数.点击链接页面都封装在PopMessage.js中,你无须修改. 3).使用简单,你只要根据你的需要修改PopMessageHandler.ashx,获取你需要提醒的数据即可. 4).页面引用简单,易懂. 下面讲述实现过程: 1.在你的Web项目中根目录中添加Com

qt5信息提示框QMessageBox用法

information QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); 这是比较常用的一种用法,效果如下: information原型: StandardButton QMessageBox::information(QWidget * parent, const QString & title,

安卓--信息提示框(Toast)实例

main.xml代码如下: <?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"