作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,请帮助我了解如何保存我的复选框状态,当我离开 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/
我是一名优秀的程序员,十分优秀!