android studio保存按钮,android-studio

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

我要做的就是即使关闭应用程序完全后也要存储增量值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值