gpt4 book ai didi

java - 即使应用程序关闭后如何保存复选框状态

转载 作者:行者123 更新时间:2023-12-02 03:04:39 25 4
gpt4 key购买 nike

大家好,请帮助我了解如何保存我的复选框状态,当我离开 Activity 、刷新或关闭应用程序时,我的复选框会重置为未选中状态。我尝试四处搜索,但无法保存,下面是我的代码

    package com.example.android.xb;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class RemindersActivity extends AppCompatActivity {

Calendar calender = Calendar.getInstance();

private TextView timePicker;


private int pHour;
private int pMinute;

static final int TIME_DIALOG_ID = 0;

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

pHour = hourOfDay;
pMinute = minute;
updateDisplay();

}
};

private void updateDisplay() {
timePicker.setText(new StringBuilder()
.append(pad(pHour)).append(":")
.append(pad(pMinute)));
}

private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);


// Display for up button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// Setting up the onlick listener
findViewById(R.id.checkbox_alert).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (((CheckBox) view).isChecked()) {


// Time setup for notification to pop up
calender.set(Calendar.HOUR_OF_DAY, pHour);
calender.set(Calendar.MINUTE, pMinute);

// Setting up the notification on checkbox checked
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(),
alarmManager.INTERVAL_DAY, pendingIntent);


}
}


});

// Setting up the onclick listener for time textView
timePicker = (TextView) findViewById(R.id.time_set);

timePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});

final Calendar calendar = Calendar.getInstance();
pHour = calendar.get(Calendar.HOUR_OF_DAY);
pMinute = calendar.get(Calendar.MINUTE);

updateDisplay();

}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, pHour, pMinute, false);

}
return null;
}
}

最佳答案

为此使用共享首选项:

要将数据写入共享首选项,请使用以下示例:

SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("checkbox", checkbox.isChecked())); //first value -preference name, secend value -preference value
editor.commit();

要读取共享首选项:

SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
boolean isMyValueChecked = sharedPref.getBoolean("checkbox", false);//first value -preference name, secend value - default value if checbox not found

关于java - 即使应用程序关闭后如何保存复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41903311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com