来自 sharedpreferences 的连续整数计数器
continuing integer counter from the sharedpreferences
要将值存储在计数器应用程序中,您必须单击一次,否则它会将您的计数器重置为默认值。
我遵循了这个答案Continuing integer counter from sharedpreferences
我不得不问,如果我关闭应用程序并再次重新打开应用程序,而不是点击一次然后再次关闭,那么我如何才能获得我的计数器的最后一次会话值。
这是代码:
public class MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener {
public static final String PREFS_NAME = "myPrefsFile";
TextView tv;
ImageButton btncount, btnreset;
int counter;
SharedPreferences myPrefs;
String stringCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvcounter);
btncount = (ImageButton) findViewById(R.id.btncount);
btnreset = (ImageButton) findViewById(R.id.btnreset);
btncount.setOnClickListener(this);
btnreset.setOnClickListener(this);
try {
myPrefs = getSharedPreferences(PREFS_NAME, counter);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
}
@Override
public void onClick(View v) {
if (v == btncount){
counter++;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
if(v == btnreset){
counter = 0;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
}
@Override
protected void onPause(){
super.onPause();
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
}
}
如果我没理解错的话,您是从 OnClick 方法中的共享首选项中获取值的。您可以在 Activity 的 onCreate 方法中初始化它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = findViewById(R.id.counter_tv);
if (savedInstanceState!=null) {
counterTextView.setText(String.parseInt(savedInstanceState.getInt("counter_key")))
}
这将使您无需等待侦听器响应即可初始化您的值。如果这还不够……您可以尝试发布您自己的代码,以便我们更好地帮助您。
编辑
感谢您的更新...我更清楚地看到您要做什么。好的,首先,您错误地获取了 SharedPreferences。它要求的 INT 是从共享首选项中读取的模式。这个开发者 API 指南在这个主题上非常清楚:https://developer.android.com/training/data-storage/shared-preferences.html#GetSharedPreferences
如该指南所述,您不必创建自己的 SharedPreference 文件,您可以获得默认文件...但如果您确实想要这样做,它应该如下所示:
SharedPreferences prefs = getSharedPreferences("myPrefFile",Context.MODE_PRIVATE);
话虽这么说,您的计数器默认为 0(== Context.MODE_PRIVATE),所以在这一点上,一旦您这样做,您将看不到任何差异。
你们非常亲密!!您没有初始化 stringCounter 变量(仅在您的 onClick 上)。这就解释了为什么它只有在您单击时才能正常工作。
try {
myPrefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
stringCounter = Integer.toString(counter);
然后在你的 onPause
@Override
protected void onPause(){
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
super.onPause();
}
总之,希望能得到你所需要的!进行此更改后,它在我的模拟器上正常工作。
要将值存储在计数器应用程序中,您必须单击一次,否则它会将您的计数器重置为默认值。
我遵循了这个答案Continuing integer counter from sharedpreferences
我不得不问,如果我关闭应用程序并再次重新打开应用程序,而不是点击一次然后再次关闭,那么我如何才能获得我的计数器的最后一次会话值。
这是代码:
public class MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener {
public static final String PREFS_NAME = "myPrefsFile";
TextView tv;
ImageButton btncount, btnreset;
int counter;
SharedPreferences myPrefs;
String stringCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvcounter);
btncount = (ImageButton) findViewById(R.id.btncount);
btnreset = (ImageButton) findViewById(R.id.btnreset);
btncount.setOnClickListener(this);
btnreset.setOnClickListener(this);
try {
myPrefs = getSharedPreferences(PREFS_NAME, counter);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
}
@Override
public void onClick(View v) {
if (v == btncount){
counter++;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
if(v == btnreset){
counter = 0;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
}
@Override
protected void onPause(){
super.onPause();
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
}
}
如果我没理解错的话,您是从 OnClick 方法中的共享首选项中获取值的。您可以在 Activity 的 onCreate 方法中初始化它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = findViewById(R.id.counter_tv);
if (savedInstanceState!=null) {
counterTextView.setText(String.parseInt(savedInstanceState.getInt("counter_key")))
}
这将使您无需等待侦听器响应即可初始化您的值。如果这还不够……您可以尝试发布您自己的代码,以便我们更好地帮助您。
编辑
感谢您的更新...我更清楚地看到您要做什么。好的,首先,您错误地获取了 SharedPreferences。它要求的 INT 是从共享首选项中读取的模式。这个开发者 API 指南在这个主题上非常清楚:https://developer.android.com/training/data-storage/shared-preferences.html#GetSharedPreferences
如该指南所述,您不必创建自己的 SharedPreference 文件,您可以获得默认文件...但如果您确实想要这样做,它应该如下所示:
SharedPreferences prefs = getSharedPreferences("myPrefFile",Context.MODE_PRIVATE);
话虽这么说,您的计数器默认为 0(== Context.MODE_PRIVATE),所以在这一点上,一旦您这样做,您将看不到任何差异。
你们非常亲密!!您没有初始化 stringCounter 变量(仅在您的 onClick 上)。这就解释了为什么它只有在您单击时才能正常工作。
try {
myPrefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
stringCounter = Integer.toString(counter);
然后在你的 onPause
@Override
protected void onPause(){
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
super.onPause();
}
总之,希望能得到你所需要的!进行此更改后,它在我的模拟器上正常工作。