zoukankan      html  css  js  c++  java
  • Mono for Android 对话框 倒计时

    UI调度:

     public class Dispatcher : Handler
        {
            public override void HandleMessage(Message msg)
            {
                var ai = msg.Obj as ActionItem;
                if (ai != null)
                {
                    try
                    {
                        ai.Result = ai.d.DynamicInvoke(ai.args);
                    }
                    catch (TargetInvocationException e)
                    {
                        ai.Error = e.InnerException;
                    }
                    catch (Exception e)
                    {
                        ai.Error = e;
                    }

                    if (ai.mre != null)
                    {
                        ai.mre.Set();
                    }
                }
            }

            /// <summary>
            /// 同步调用
            /// </summary>
            public object Invoke(Delegate d, params object[] args)
            {
                if (Java.Lang.Thread.CurrentThread().Equals(Looper.Thread))
                {
                    return d.DynamicInvoke(args);
                }

                var ai = new ActionItem
                {
                    d = d,
                    args = args,
                    mre = new System.Threading.ManualResetEvent(false)
                };

                SendMessage(new Message { Obj = ai });

                ai.mre.WaitOne();

                if (ai.Error != null)
                {
                    throw ai.Error;
                }

                return ai.Result;
            }

            /// <summary>
            /// 异步调用
            /// </summary>
            public void BeginInvoke(Delegate d, params object[] args)
            {
                var msg = new Message()
                {
                    Obj = new ActionItem
                    {
                        d = d,
                        args = args
                    }
                };
                SendMessage(msg);
            }

            class ActionItem : Java.Lang.Object
            {
                public Delegate d;

                public object[] args;

                public object Result;

                public Exception Error;

                public System.Threading.ManualResetEvent mre;
            }
        }

    String.xml中的样式:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       

      <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>

        <!--<item name="android:windowIsTranslucent">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/black</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:backgroundDimEnabled">false</item>-->
      </style>
     
    </resources>

    倒计时代码:

     

        public class TimerDialog
        {
            #region 成员

            AlertDialog mDialog;
            Context mContext;
            int mId = 100;
            Dispatcher dp = new Dispatcher();
            Timer timer;
            public int Seconds { get; set; }

            #endregion

            #region 方法

            /// <summary>
            /// 初始化
            /// </summary>
            public TimerDialog(Context ctx)
            {
                Seconds = 30;
                mContext = ctx;
                mDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, Resource.Style.dialog)).Create();

                TextView text = new TextView(mContext);
                text.Id = mId;
                text.TextSize = 120;
                text.Text = Seconds.ToString();
                //字体白色
                text.SetTextColor(Android.Graphics.Color.White);
                //居中
                text.Gravity = GravityFlags.Center;

                mDialog.SetView(text);

                //阻止点击别的地方导致对话框关闭

                mDialog.SetCancelable(false);  

          }
      
            /// <summary>
            /// 显示对话框
            /// </summary>
            public void Show()
            {
                mDialog.SetIcon(Android.Resource.Drawable.IcDialogInfo);
                Start();
                mDialog.Show();
            }

            /// <summary>
            /// 开始倒计时
            /// </summary>
            public void Start()
            {
                Stop();
                timer = new Timer(AFunction, null, 0, 1000);
            }

            /// <summary>
            /// 倒计时中
            /// </summary>
            private void AFunction(object obj)
            {
                if (Seconds > 0)
                {
                    Seconds -= 1;
                    dp.BeginInvoke(new Action(() =>
                    {
                        (mDialog.FindViewById(mId) as TextView).Text = (Seconds + 1).ToString();
                    }));
                }
                else
                {
                    Stop();
                    dp.BeginInvoke(new Action(() =>
                    {
                       mDialog.Dismiss();
                       mDialog.Dispose();
                       mDialog = null;
                    }));
                }
            }

            /// <summary>
            /// 停止倒计时
            /// </summary>
            public void Stop()
            {
                if (timer != null)
                {
                    timer.Dispose();
                    timer = null;
                }
            }

            #endregion
        }

    Activity的调用:

     TimerDialog c = new TimerDialog(this);
                    c.Show();

  • 相关阅读:
    注解实现SpringCache自定义失效时间(升级版)
    表白小游戏之——制作一个小游戏给喜欢的人(Cocos Creator入门小案例)
    3.python编程与计算机的关系,如何执行python文件
    如何临时发布部署Cocos小游戏到Linux服务器,让别人能在微信打开
    当互联网公司换上东京奥运会图标
    灵魂画手的零基础python教程1:关于Python学习的误区、python的优缺点、前景
    聊一聊关于聊天记录的存储
    【爬虫系列】1. 无事,Python验证码识别入门
    【爬虫系列】0. 无内鬼,破解前端JS参数签名
    JPA
  • 原文地址:https://www.cnblogs.com/Cindys/p/2735466.html
Copyright © 2011-2022 走看看