setText() 方法在用户退出后重置 activity

setText() method resetting after user exits activity

因此,当用户单击按钮时,我希望它将文本设置为将来的时间。它可以工作,但是当用户关闭 activity 时,按钮会重置。另外,如果有人也可以指导我创建一种在到达时间后重置按钮文本的方法。这是我在按钮上设置文本的代码。

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);


}

您应该将应用程序的状态(按钮、时间等)保存在 SharedPreferences 中。重新加载时 activity 从中读取

使用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