因此,当用户单击按钮时,我希望它将文本设置为将来的时间。它可以工作,但当用户关闭活动时,按钮会重置。此外,如果有人也可以指导我创建一个方法,在达到时间后重置按钮文本。继承我按钮上的setText代码。
private void setReloadTime(int reload, Button btn) {
int minutes = (reload/1000/60)%60;
int hours = reload/1000/60/60;
Calendar c = Calendar.getInstance();
int min = c.get(Calendar.MINUTE);
int hr = c.get(Calendar.HOUR_OF_DAY);
if (reload ==0){
btn.setTextColor(R.color.colorAccent);
btn.setText("Ready Again In 24 Hours");
return;
}
if (minutes+min>60) hr++;
int finMin = (minutes + min) %60;
int finHr= (hours + hr)%24;
String fin = "Ready again at " +finHr + ":";
if (finMin<10){
fin = fin +"0"+finMin;
} else{
fin = fin + finMin;
}
btn.setTextColor(R.color.colorAccent);
btn.setText(fin);
}
答案 0 :(得分:1)
您应该在SharedPreferences中保存应用程序的状态(按钮,时间等)。关于从
读取的重新加载活动答案 1 :(得分:1)
使用shared preference
例如:
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString(Name, fin);
editor.commit();
if (sharedpreferences.contains(Name))
{
btn.setText(sharedpreferences.getString(Name, ""));
}
http://www.tutorialspoint.com/android/android_shared_preferences.htm