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

 

 

转载于:https://www.cnblogs.com/Cindys/archive/2012/10/23/2735466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值