隐藏

如何使用Xamarin.Android中的倒计时类设置、重置计时器

发布:2021/10/13 16:57:48作者:管理员 来源:本站 浏览次数:1388



我为另一个项目制作了一个简单的倒计时应用程序,我尽力运行这个简单的计时器来启动、暂停和重置timer.My第一个问题是,由于MainActivity不能继承多个类,所以我必须创建一个新的类,这样我才能继承抽象类倒计时,其次,我不完全知道我是否能够从抽象类中调用倒计时构造函数,因为我不能初始化一个抽象类,只能在子类构造函数中调用它的形式。而且,我不明白我的UI.I'm在这个问题上完全迷失了,有人能帮我吗。我也在github上发布了代码


GitHub:https://github.com/owais19m/TimerApp_Test


Thanks ,


   Blockquote


using Android.App;

using Android.OS;

using Android.Support.V7.App;

using Android.Runtime;

using Android.Widget;

using Android.Content;

using Android.Telephony;

using Android.Provider;

using Android.Util;

using Java.Lang;

using System.Text.RegularExpressions;

using Xamarin.Essentials;

using System;

using Android;

using Android.Support.V4.Content;

using Android.Content.PM;

namespace TimerApp_Test

{

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

    public class MainActivity : AppCompatActivity

    {

        private static System.Boolean mTimerRunning;

    protected override void OnCreate(Bundle savedInstanceState)

    {

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);

        // Set our view from the "main" layout resource

        SetContentView(Resource.Layout.activity_main);

        //------------ Views -------------

        Button ResetBtn = FindViewById<Button>(Resource.Id.ResetButton);

        Button StartBtn = FindViewById<Button>(Resource.Id.StartButton);

        TextView TimerText = FindViewById<TextView>(Resource.Id.CountdownText);

        //--------------------------------

        StartBtn.Click += (s, e) =>

        {

               

             if (mTimerRunning)

            {

                pauseTimer();

            }

            else

            {

                startTimer();

            }

               

        };

        ResetBtn.Click += (s, e) =>

        {

            resetTimer();

        };

    }

    public void startTimer()

    {

        CountDown1 Coutdwn = new CountDown1(6000,1000);

        Coutdwn.OnTick(1000);

           

        Coutdwn.Start();

        mTimerRunning = true;  

               

           

    }

     

    public void resetTimer()

    {

    }

    public void pauseTimer()

    {

           

    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)

    {

        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    }

     

}

public class CountDown1 : CountDownTimer

{

    public static long Start_Time_Milliseconds = 600000;

    public static CountDownTimer mcountDownTimer;

       

    public static long mTimeLeftInMillis = Start_Time_Milliseconds;

    public CountDown1(long mTimeLeftInMilli, long countDownInterva) : base(mTimeLeftInMilli,countDownInterva)

    {

    }

    public override void OnFinish() { }

         

       

    public override void OnTick(long millisUntilFinished)

    {

        mTimeLeftInMillis = millisUntilFinished;

        updateCountDownText();


    }

    public string updateCountDownText()

    {

        int minutes = (int)mTimeLeftInMillis / 1000 / 60;

        int seconds = (int)mTimeLeftInMillis / 1000 % 60;

        string timeLeftFormatted = string.Format("%02d:%02d", minutes, seconds);

        return timeLeftFormatted;

    }

    public void Cancellation()

    {

        mcountDownTimer.Cancel();

    }

}


}


   Blockquote