我试图让按钮增加一个计数器,并将该计数器保存到共享的首选项中,以便即使退出应用程序,计数器的值也不会重置为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:
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">
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" />
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" />
我要做的就是即使关闭应用程序完全后也要存储增量值。