Я пытаюсь заставить кнопку увеличить счетчик и сохранить счетчик в общих настройках, чтобы даже при выходе из приложения значение счетчика не сбрасывалось до 0. Я пытался использовать общие настройки, но значение просто не сохраняется . Я просмотрел другие похожие формы, но не могу понять, как реализовать их в моей версии кода.
Вот код:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Screen2 extends AppCompatActivity {
TextView showValue;
int counter = 0;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen2);
showValue = findViewById(R.id.AnnieCount);
//showValue.setVisibility(View.INVISIBLE);
sharedpreferences = getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
public void AnCount(View v) {
//increase the count
counter++;
showValue.setText(Integer.toString(counter));
SharedPreferences.Editor editor = sharedpreferences.edit ();
editor.putInt("annie", Integer.valueOf(counter));
// openDialog();
}
РЕДАКТИРОВАТЬ
Вот минимальный рабочий код и XML:
Основная деятельность :
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView showValue;
int counter = 0;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showValue = findViewById(R.id.AnnieCount);
//showValue.setVisibility(View.INVISIBLE);
sharedpreferences = getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
}
public void AnCount(View v) {
//increase the count
counter++;
showValue.setText(Integer.toString(counter));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("counter", counter);
editor.commit();
// openDialog();
}
}
Вот XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/AnnieCount"
android:layout_width="57dp"
android:layout_height="43dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="145dp"
android:layout_marginEnd="167dp"
android:layout_marginRight="167dp"
android:gravity="center"
android:text="0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/AnnieBtn"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/AnnieBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginLeft="72dp"
android:layout_marginTop="145dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:onClick="AnCount"
android:text="annie_liou"
app:layout_constraintEnd_toStartOf="@+id/AnnieCount"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Все, что мне нужно сделать, это сохранить значение приращения даже после полного закрытия приложения.