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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274554.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274554.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274554.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

熊kong作品資源鏈接_Kong雀技術:向世界展示您的設計作品

熊kong作品資源鏈接The door opened and I entered the bedroom of an apartment I was looking to rent. No furniture or items inside, it was almost empty except for a frame in the wall. It was a photo of a peacock. As I stared at it, I could not shake one clear…

漫談前端工程化基建和架構設計 | 留言送書

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。本文留言抽獎送書&#xff0c;具體規則看文末。透過工程基建&#xff0c;架構有跡可循。前…

oracle中 rownum與rowid的理

一、 Oracle分頁查詢 我們先看學習一下oracle分頁查詢的語法示例&#xff0c;然后在具體學習用rownum的原理。 /*從第1條開始&#xff0c;每次選N個&#xff0c;從第1M個開始每次選N個*/ /**/ select t2.* from (select rid from (select r.rid, rownum linenum from (select r…

設計模式 日志系統設計_模式:我們設計系統的故事

設計模式 日志系統設計Design Patterns are some of the most over-used concepts in design today. And we all know what happens when you have some ideas all over the place. We start repeating them like parrots and applying them to everything, therefore distorti…

前端好還是后端好,看看7年前端和后端怎么說

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

提升UI技能的5個步驟

element ui 步驟重點 (Top highlight)What to do when you know how to use the software and know the basics of designing interfaces? There are a few simple things that you can do to take your skills to the next level, and you don’t need to invest in expensiv…

空降進阿里的 P10 都是什么人

周末見了幾個朋友&#xff0c;吃飯時聊到他們前老板郭東白&#xff08;阿白&#xff09;&#xff0c;對了&#xff0c;我朋友在速賣通&#xff0c;他說阿白是 14 年來的阿里&#xff0c;直接就空降進了他們部門&#xff0c;當上首席架構師&#xff0c;后來又升到了 CTO&#xf…

linux下練習 c++ 關聯式容器multimap特性

/* multimap特性 key可以重復 不支持下標訪問 */ #include<iostream> #include<string> #include "print.h" #include<map> using namespace std; typedef pair<int,string> pairmp; typedef multimap<string,double> MS;int main() …

一致性設計,而不是一致性

一致性設計重點 (Top highlight)If we ask any design system advocate what are the main reasons to build and maintain a design system, chances are ‘Consistency’ will come up as first or second in their list, together with the ‘A single source of truth’ po…

如何在 React 應用中使用 Hooks、Redux 等管理狀態

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系…

長語音識別體驗_如何為語音體驗寫作

長語音識別體驗重點 (Top highlight)“Voice User Interface (VUI) Designer” is an increasingly prominent job title in the tech world. A VUI designer typically writes the conversation and designs the flow between a VUI — an invisible interface that communica…

表連接

初學SQL表連接的時候&#xff0c;什么笛卡爾積&#xff0c;左連接&#xff0c;右連接看的頭都大了 后來看了《SQL Server技術內幕2008&#xff1a;T-SQL查詢》之后&#xff0c;豁然開朗。今天寫數據庫又用到了表連接&#xff0c;印象有點模糊了&#xff0c;趕緊找地方寫下來先。…

分析了1011個程序員的裁員情況后得出的啟示

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系…

定義設計系統

System is “the whole creation, the universe,” from Late Latin systema “an arrangement, system,” from Greek systema “organized whole, a whole compounded of parts”.系統是晚期拉丁語系統的“整體創造物&#xff0c;宇宙”&#xff0c;是希臘語系統的“一種安排…

如何備份linux系統(轉)

如何備份linux系統 不像Windows&#xff0c;Linux不限制根用戶存取任何東西&#xff0c;因此&#xff0c;你完全可以把一個分區上每一個的文件放入一個TAR文件中。來實施這一方法&#xff0c;用這個成為根用戶&#xff1a;sudo su接著去你的文件系統的根目錄&#xff08;在我們…

2w行代碼、200個實戰項目,助你修煉5大編程基本功。【送書《設計模式之美》】...

大家好&#xff0c;我是若川。之前送了很多書&#xff0c;現在又和異步圖書合作再次爭取了幾本書&#xff0c;具體送書規則看文末。所謂練武不練功&#xff0c;到老一場空&#xff0c;以技術為驅動的程序員同樣如此。面向對象編程范式、設計原則、代碼規范、重構技巧和設計模式…

C++第10周項目2擴展之2參考——迭代求和

課程首頁地址&#xff1a;http://blog.csdn.net/sxhelijian/article/details/7910565【項目2擴展之2&#xff08;選做&#xff09;】計算下面的式子&#xff0c;不能使用求冪函數pow()式一&#xff1a;#include <iostream> using namespace std; int main( ) { int i,m1;…

swift自行車品牌介紹_品牌101:簡介

swift自行車品牌介紹Sometimes when I’m around designer friends and there’s a lull in the conversation one of us will blurt out, “What is branding, anyway?” Then we shrug our shoulders and chuckle, knowing that the answer is far too complex to sum up in…

flutter 透明度動畫_Flutter中的動畫填充+不透明度動畫?

flutter 透明度動畫Flutter SDK provides us with many widgets which help us in animating elements on screen easily by implicitly managing the animations i.e. we need not worry about creating and managing intances of AnimationController during the lifecycle o…

阿里 P10 是怎樣的存在?

談起中國頂尖的程序員&#xff0c;很多人首先會想到之前的雷軍、張小龍&#xff0c;還有現在的多隆、行癲、道哥等人&#xff0c;但今天我想聊一聊的這位大神&#xff0c;他的技術成就也同樣令人矚目。19 年獲得國家技術發明二等獎、20 年獲得國家計算機協會頒發的“ CCF 杰出工…