在textview中显示转换时间,以毫秒为单位

时间:2014-04-18 22:59:22

标签: c# android visual-studio

我正在制作计时器应用程序。根据我现在的情况,用户可以从微调器中选择:小时,分钟和秒。我有一个公式将计算器的总选择时间转换为毫秒。此外,时间应转换为字符串并显示。问题是没有显示任何内容。 TextView倒计时应显示以小时,分钟和秒为单位的时间。

using System;
using System.Timers;

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

namespace QuickTimer
{
    [Activity(Label = "QuickTimer", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        private TimeSpan _ts;
        private Timer _timer = new Timer();
        private int _counter;
        private string _time;
        private int _convertHours;
        private int _convertMinutes;
        private int _convertSeconds;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Button buttonSet = FindViewById<Button>(Resource.Id.setButton);
            buttonSet.Click += setButton_Click;

            Spinner spinnerHours = FindViewById<Spinner>(Resource.Id.spinnerHours);

            spinnerHours.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerHours_ItemSelected);
            var adapterHours = ArrayAdapter.CreateFromResource(
                this, Resource.Array.hoursArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterHours.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerHours.Adapter = adapterHours;

            Spinner spinnerMinutes = FindViewById<Spinner>(Resource.Id.spinnerMinutes);

            spinnerMinutes.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerMinutes_ItemSelected);
            var adapterMinutes = ArrayAdapter.CreateFromResource(
                this, Resource.Array.minutesArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterMinutes.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerMinutes.Adapter = adapterMinutes;

            Spinner spinnerSeconds = FindViewById<Spinner>(Resource.Id.spinnerSeconds);

            spinnerSeconds.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinnerSeconds_ItemSelected);
            var adapterSeconds = ArrayAdapter.CreateFromResource(
                this, Resource.Array.secondsArray, Android.Resource.Layout.SimpleSpinnerItem);

            adapterSeconds.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinnerSeconds.Adapter = adapterSeconds;

            _timer.Interval = 1000;
            _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            _timer.Enabled = true;
        }

        private void setButton_Click(object sender, EventArgs e)
        {
            TextView countdown = (TextView)FindViewById(Resource.Id.countdown);
            countdown.Text = getTime();
        }

        public string getTime()
        {
            return _time;
        }

        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            _counter = (getConvertedTime()/1000);
            Console.WriteLine(getConvertedTime());
            for (int i = 0; i <= 5/*(getConvertedTime()/1000)*/; i++)
            {
                _ts = TimeSpan.FromSeconds(_counter);
                string _time = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
                    _ts.Hours,
                    _ts.Minutes,
                    _ts.Seconds,
                    _ts.Milliseconds);
                Console.WriteLine(_time);
                _counter--;
            }

            if (_counter == 0)
            {
                _timer.Enabled = false;
            }
        }

        public int getConvertedTime()
        {
            int convertedTime = _convertHours + _convertMinutes + _convertSeconds;
            return convertedTime;
        }

        private void spinnerHours_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinnerHours = (Spinner)sender;

            int hours = Int32.Parse(spinnerHours.SelectedItem.ToString());
            _convertHours = hours * 1000 * 3600;

            string toast = string.Format("", spinnerHours.GetItemAtPosition(e.Position));
            Toast.MakeText(this, toast, ToastLength.Long).Show();
        }

        private void spinnerMinutes_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinnerMinutes = (Spinner)sender;

            int minutes = Int32.Parse(spinnerMinutes.SelectedItem.ToString());
            _convertMinutes = minutes * 1000 * 60;

            string toast = string.Format("", spinnerMinutes.GetItemAtPosition(e.Position));
            Toast.MakeText(this, toast, ToastLength.Long).Show();
        }

        private void spinnerSeconds_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinnerSeconds = (Spinner)sender;

            int seconds = Int32.Parse(spinnerSeconds.SelectedItem.ToString());
            _convertSeconds = seconds * 1000;

            string toast = string.Format("", spinnerSeconds.GetItemAtPosition(e.Position));
            Toast.MakeText(this, toast, ToastLength.Long).Show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我必须添加一个RunOnUiThread调用