保存按钮在Xamarin的Andr​​oid状态按钮、状态、Andr、Xamarin

2023-09-03 21:36:36 作者:猛虎隱身山

我禁用按钮,一旦他们pressed。我希望它会自动激活 第二天12AM。我真的不知道如何做到这一点。不知怎的,我试过,并写入code以下。

在code禁用后,点击按钮。但它失去了他的状态,一旦应用程序被关闭。请帮帮我,我该怎么办呢?

 公共类MainActivity:活动
    {


        按钮按钮1;
        按钮按钮2;



        保护覆盖无效的OnCreate(束savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //从主的布局资源设置我们的观点
            的setContentView(Resource.Layout.Main);
            mybroadcast myrec =新mybroadcast();
            数据库sqldb1 =((GlobalClass)this.Application).sqldb; //这将包含的Hello World
            字符串stringFromApplicationClass =((GlobalClass)this.Application).myString; //这将包含的Hello World
            变种现在= DateTime.Now;
            字符串dataFormatada =的String.Format({0:00} / {1:00} / {2:0000},now.Month,now.Day,now.Year);
            字符串currentTime的=(的String.Format(当前时间:{0},now.Hour));
            //获取我们的按钮从布局资源,
            //并附加一个事件,它
            按钮1 = FindViewById<按钮> (Resource.Id.Button1);
            按钮2 = FindViewById<按钮> (Resource.Id.Button2);


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

            };


            button2.Click + =委托{

                sqldb1.AddRecord(0);
            };
        }


        公共无效启动()
        {
            意图myIntent =新的意图(此的typeof(mybroadcast));

            AlarmManager alarmMgr =(AlarmManager)this.GetSystemService(Context.AlarmService);
            PendingIntent pendingIntent = PendingIntent.GetService(此,0,myIntent,0);
            台历挂历= 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); //重复每24小时

        }



        公共类mybroadcast:BroadcastReceiver的
        {
            公众覆盖无效的onReceive(上下文的背景下,意图myIntent)
            {
                ((MainActivity)上下文).enablebutton();
            }
        }
        公众覆盖布尔OnCreateOptionsMenu(IMenu菜单)
        {
            base.OnCreateOptionsMenu(菜单);

            MenuInflater充气= this.MenuInflater;

            inflater.Inflate(Resource.Menu.items,菜单);

            返回base.OnCreateOptionsMenu(菜单);
        }

        公众覆盖布尔OnOptionsItemSelected(IMenuItem项)
        {
            base.OnOptionsItemSelected(项目);

            开关(item.ItemId)
            {
            案例Resource.Id.week:
                StartActivity(typeof运算(SecondActivity));
                打破;
            案例Resource.Id.month:
                {
                    StartActivity(typeof运算(ThirdActivity));
                    打破;
                }
            默认:
                打破;
            }

            返回true;

        }



        公共无效disablebutton()
        {

            button1.Enabled = FALSE;
            button2.Enabled = FALSE;
        }


        公共无效enablebutton()
        {

            button1.Enabled = TRUE;
            button2.Enabled = TRUE;
        }
        }
 

解决方案

尽管这code是在Java中,这个概念是相同的。一旦警报响起,该的BroadcastReceiver的的onReceive叫,在​​那里你可以编辑的共享preference启用/禁用的观点。如果你不知道的共享preferences,请参见以下链接:How我在Xamarin.Android使用共享preferences?,的共享preferences例Xamarin的Andr​​oid

BroadcastReceiver的:

 公共类ReminderActivity扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    //使用共享preference,设置禁用或启用按钮状态。
    共享preferences mShared preferences = getShared preferences(我的preF,0);
    。mShared preferences..edit()putBoolean(btn_enable,真).commit();

}
}
 

所以一旦你打开应用程序,检查共享preference值,并启用或禁用按钮状态相应。 希望这有助于。

I'm disabling buttons once they are pressed. I want it to activate automatically the next day at 12AM. 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. Once the alarm goes off, the Broadcastreceiver's onReceive is called, where you can edit the sharedpreference to enable/disable views. If you don't know about sharedpreferences, see these links: How do I use SharedPreferences in Xamarin.Android? , SharedPreferences Example in Xamarin Android

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. Hope this helps.