java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版

时间:2022-01-22 22:03:33

群里的朋友问利用CountDownTimer类实现倒计时功能怎么实现,我就百度了,参考http://blog.csdn.net/qq344429461/article/details/7521361写了个mono版的.

本人菜鸟,请各位多多指点,不足之处,请斧正.没啥技术含量,就权当丰富下mono for android的小代码.

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Java.Util;

namespace AndroidApplication5
{
    [Activity(Label = "AndroidApplication5", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        private MyCount mc;
        private TextView tv;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            tv = (TextView)FindViewById(Resource.Id.MyButton);   
            mc = new MyCount(this,70000, 1000);
            mc.Start();
        }
        public class MyCount : CountDownTimer
        {
            private Activity context = null;
            TextView tv ;
            public  MyCount(Activity Activity,long millisInFuture, long countDownInterval): base(millisInFuture, countDownInterval)
            {
                this.context = Activity;
                tv = (TextView)context.FindViewById(Resource.Id.MyButton);  
            }
            public override void OnTick(long millisUntilFinished)
            { 
                DateTime date=new DateTime(millisUntilFinished);
                String str = date.ToString();
                Console.WriteLine(str);
                tv.SetText("请等待70秒(" + millisUntilFinished / 1000 + ")...", TextView.BufferType.Normal);  
            }
            public override void OnFinish()
            {
                tv.Text = "finish";
            }
        }
        public override void Finish()
        {
            base.Finish();
            mc.Cancel();
        }  
    }
}

原java版

public class MainActivity extends Activity {  
    private MyCount mc;  
    private TextView tv;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        tv = (TextView)findViewById(R.id.show);   
        mc = new MyCount(70000, 1000);   
        mc.start();   
    }  
    class MyCount extends CountDownTimer{  
  
        public MyCount(long millisInFuture, long countDownInterval) {  
            super(millisInFuture, countDownInterval);  
            System.out.println("你好");  
        }  
        @Override  
        public void onTick(long millisUntilFinished) {  
            Date date = new Date(millisUntilFinished);  
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");  
            String str = sdf.format(date);  
            System.out.println(str);  
            tv.setText("请等待70秒("+millisUntilFinished / 1000 +")...");  
        }  
        @Override  
        public void onFinish() {  
            tv.setText("finish");  
        }  
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        mc.cancel();  
    }  
}