你好,任何人都可以请帮助如何保存我的复选框状态,当我离开活动,刷新或关闭应用程序,我的ckeckbox重置为未选中。我试着搜索,但我无法使它保存,下面是我的代码
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;
}
}
4条答案
按热度按时间z9ju0rcb1#
为此使用共享首选项:
要将数据写入共享首选项,请使用以下命令:
要从共享首选项读取:
hsgswve42#
在这里你可以看到我为这个问题编写的完整Java代码:在onCreate Method()中放入这些代码,你的问题就会得到解决。这里的Activity称为SettingsActivity。
oo7oh9g93#
你必须像Micha、skbrhmn和Sabriael建议的那样使用SharedPreferences。按照下面的链接中的指南来帮助您如何做到这一点。
https://developer.android.com/training/basics/data-storage/shared-preferences.html
有关SharedPreferences界面的更多详细信息...
https://developer.android.com/reference/android/content/SharedPreferences.html
2ledvvac4#
您可以在每次更新结果时将其保存在SharedPreferences中。即使您的应用程序被杀死,它也会存储在那里。您可以在每次再次启动Activity时检查SharedPreferences中的值,并相应地设置检查。
设置SharedPreference:
更新或编辑sharedPreferences,您可以在更新复选按钮时或在onClick期间将其添加到:
您可以从sharedPreference中读取:
您也可以查看Android文档或此tutorial。