不飞则已,一飞冲天;不鸣则已,一鸣惊人。这篇文章主要讲述Xamarin.Android 使用Timer 并更改UI相关的知识,希望能为你提供帮助。
【Xamarin.Android 使用Timer 并更改UI】http://blog.csdn.net/ozhangsan12345/article/details/72653070
第一步:创建timer对象
[html] view plain copy
- //创建timer对象
- Timer _dispatcherTimer;
- //计数
- int sec = 60;
第二步: 实例化timer并给委托事件
[html] view plain copy
- TimerCallback timerDelegate = new TimerCallback(Tick); //tick为执行防范
- _dispatcherTimer = new Timer(timerDelegate, null, 0, 1000);
[html] view plain copy
- public void Tick(object state)
- {
- this.RunOnUiThread(() =>
- {
- if (sec > 0)
- {
- smsbt.Text = sec.ToString() + "秒可重发";
- sec--;
- }
- else
- {
- _dispatcherTimer.Dispose();
- sec = 60;
- smsbt.Text = "获取验证码";
- }
- });
- }
//使用
[html] view plain copy
- {
- TimerCallback timerDelegate = new TimerCallback(Tick);
- _dispatcherTimer = new Timer(timerDelegate, null, 0, 1000);
- ProgressDialog progressDialog = ProgressDialog.Show(this, "", "请稍后...");
- new Thread(new ThreadStart(() =>
- {
- string url = this.GetString(Resource.String.url) + "/AppServices/userServices.aspx?action=regSms";
- using (var http = new HttpClient())
- {
- var content = new FormUrlEncodedContent(new Dictionary< string, string> () {
- { "phone",userphone.Text }
- });
- var response = http.PostAsync(url, content);
- string me = response.Result.Content.ReadAsStringAsync().Result;
- progressDialog.Dismiss();
- this.RunOnUiThread(() =>
- {
- HandleResult(me);
- });
- }
- })).Start();
- }
推荐阅读
- Android动态权限申请
- [学习笔记] CS131 Computer Vision: Foundations and Applications(Lecture 4 像素和滤波器)
- Android-studio连接真机 调试weex项目
- androidwebview timeout
- Java Applet 基础
- call()和apply()方法
- Android 使用Retrofit2.0+OkHttp3.0实现缓存处理+Cookie持久化第三方库
- AndroidStudioAndroidStudio中出现Failed to resolve:com.android.support:appcompat-v7:报错处理
- Android APP 性能优化的一些思考