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();

  • 相关阅读:
    ARP 协议
    3GPP 5G UPF
    OpenStack v.s. Kubernetes
    C#- FTP递归下载文件
    C#- WinForm获取 当前执行程序路径的几种方法
    C#- 布署WinForm程序
    Delphi- 连接MySQL数据库BDE
    Delphi- 内置数据库的使用例子BDE
    CSS- 兼容样式记录
    Delphi- DLL操作
  • 原文地址:https://www.cnblogs.com/Cindys/p/2735466.html
Copyright © 2011-2022 走看看