Java 我如何使它工作并在退出时保存,在重新加载时恢复

Java 我如何使它工作并在退出时保存,在重新加载时恢复,java,android,function,save,Java,Android,Function,Save,现在我已经让我的按钮计数器工作,它会增加点击,但我现在的问题是它不会保存,所以当它重新打开时,它会重新开始。。。。我会把我的代码放在下面,但如果您能帮我添加保存功能,我们将不胜感激 package com.example.counter; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener

现在我已经让我的按钮计数器工作,它会增加点击,但我现在的问题是它不会保存,所以当它重新打开时,它会重新开始。。。。我会把我的代码放在下面,但如果您能帮我添加保存功能,我们将不胜感激

package com.example.counter;

import android.app.Activity; import android.os.Bundle; import android.view.View; import         android.view.View.OnClickListener; import android.widget.Button; import     android.widget.TextView;

public class MainActivity extends Activity {

// Private member field to keep track of the count
private int mCount = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
final Button countButton = (Button) findViewById(R.id.ButtonCount);

countButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mCount++;
        countTextView.setText("Count: " + mCount);
    }
});

}
}
xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/TextViewCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/ButtonCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ButtonCount"
    android:layout_alignRight="@+id/ButtonCount"
    android:layout_marginBottom="61dp"
    android:text="Count" />

</RelativeLayout>

重载onPause方法并使用SharedReferences类保存变量。您还需要重载onResume方法以将该值重新加载到mCount中

请阅读:


此链接将帮助您。这是存储数据最简单的方法

你可以在onCreate读取计数器数据,并在onPause保存,这样当用户再次打开应用程序时,计数器数据可以很好地恢复。详情请浏览


不要每次单击按钮时都保存计数器,这将导致您的简单任务的I/O过多,耗尽电池。

刚刚阅读了你们两个给我的网站,我再次感到困惑:p不要试图听起来像木偶,您可以编辑代码吗?我知道这听起来很糟糕,但我真的不知道我在做什么编辑Montycarlo的答案来纠正一些小错误,使用它。它会工作得很好。我刚读了你们两个给我的那些网站,又一次把我弄糊涂了:P不想听起来像个木偶,你能编辑代码吗?我知道这听起来很糟糕,但我真的不知道我在做什么嘿,詹姆斯,更新了代码。请再看一看。将此添加到MainActivity类。嘿,感谢您提供的代码,从原始代码中,我需要在哪里替换它?我试过几个点,但每次都会出错好,我把它复制粘贴到你说的地方,然后它崩溃了:(
public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
private SharedPreferences.Editor editor = settings.edit();

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first
    mCount = settings.getInt("mCount", 0);
}

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first
    editor.putInt("mCount", mCount);
    editor.commit();
}