简体   繁体   English

在Xamarin Android中保存按钮的状态

[英]Save the state of button in Xamarin Android

I'm disabling buttons once they are pressed. 按下按钮后,我将禁用它们。 I want it to activate automatically the next day at 12AM. 我希望它第二天凌晨12点自动激活。 I really have no idea how to do this. 我真的不知道该怎么做。 Somehow I tried and written the code below. 我以某种方式尝试并编写了以下代码。

The code disables the buttons after a click. 该代码会在单击后禁用按钮。 But it is looses his state once the app is closed. 但是,一旦应用程序关闭,它就会失去他的状态。 Please help me how can I do this? 请帮我怎么做?

public class MainActivity : Activity
    {


        Button button1;
        Button button2;



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

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            mybroadcast myrec = new mybroadcast ();
            Database sqldb1 = ((GlobalClass)this.Application).sqldb;//This will contain "Hello World"
            string stringFromApplicationClass = ((GlobalClass)this.Application).myString;//This will contain "Hello World"
            var now = DateTime.Now;
            string dataFormatada = string.Format("{0:00}/{1:00}/{2:0000}", now.Month, now.Day, now.Year);
            string currentTime = (string.Format ("Current Time: {0}", now.Hour));
            // Get our button from the layout resource,
            // and attach an event to it
            button1 = FindViewById<Button> (Resource.Id.Button1);
            button2 = FindViewById<Button> (Resource.Id.Button2);


            button1.Click += delegate {
                sqldb1.AddRecord (1);

            };


            button2.Click += delegate {

                sqldb1.AddRecord (0);
            };
        }


        public void start()
        {
            Intent myIntent = new Intent (this,typeof( mybroadcast)); 

            AlarmManager alarmMgr = (AlarmManager) this.GetSystemService(Context.AlarmService);
            PendingIntent pendingIntent = PendingIntent.GetService(this, 0, myIntent, 0);
            Calendar calendar =  Calendar.GetInstance (Java.Util.TimeZone.Default);

            calendar.Set(CalendarField.HourOfDay, 12);
            calendar.Set(CalendarField.Millisecond, 00);
            calendar.Set(CalendarField.Second, 00);
            alarmMgr.SetRepeating(AlarmType.Rtc,0, 10, pendingIntent); //Repeat every 24 hours

        }



        public class mybroadcast:BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent myIntent)
            {
                ((MainActivity)context).enablebutton();
            }
        }
        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            base.OnCreateOptionsMenu (menu);

            MenuInflater inflater = this.MenuInflater;

            inflater.Inflate (Resource.Menu.items, menu);

            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected (IMenuItem item)
        {
            base.OnOptionsItemSelected (item);

            switch (item.ItemId)
            {
            case Resource.Id.week:
                StartActivity(typeof(SecondActivity));
                break;
            case Resource.Id.month:
                {
                    StartActivity(typeof(ThirdActivity));
                    break;
                }
            default:
                break;
            }

            return true;

        }



        public void disablebutton()
        {

            button1.Enabled = false;
            button2.Enabled = false;
        }


        public void enablebutton()
        {

            button1.Enabled = true;
            button2.Enabled = true;
        }
        }

Even though this code is in java, the concept will be the same. 即使此代码在Java中,其概念也相同。 Once the alarm goes off, the Broadcastreceiver's onReceive is called, where you can edit the sharedpreference to enable/disable views. 警报响起后,将调用Broadcastreceiver的onReceive,您可以在其中编辑sharedpreference以启用/禁用视图。 If you don't know about sharedpreferences, see these links: How do I use SharedPreferences in Xamarin.Android? 如果您不了解sharedpreferences,请参阅以下链接: 如何在Xamarin.Android中使用SharedPreferences? , SharedPreferences Example in Xamarin Android Xamarin Android中的SharedPreferences示例

BroadCastReceiver: BroadCastReceiver:

public class ReminderActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    //Use sharepreference and set the button state disabled or enabled.
    SharedPreferences mSharedPreferences = getSharedPreferences("MyPref", 0);
    mSharedPreferences..edit().putBoolean("btn_enable", true).commit();

}
}

So once you open the app, check for sharedpreference value and enable or disable the button state accordingly. 因此,一旦打开应用程序,请检查sharedpreference值并相应地启用或禁用按钮状态。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM