如何使用SharedPreferences保存int?

时间:2014-10-29 18:04:27

标签: java xml sharedpreferences android

我查了很多关于如何保存int变量的教程,但我找不到好的例子。我的代码如下所示:

package com.example.countryclicker;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    int exp,level,count,totalCount;
    Button mainButton;
    TextView current,total;
    SharedPreferences pref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainButton = (Button) findViewById(R.id.button1);
        current = (TextView) findViewById(R.id.TcurrentCount);
        total =  (TextView) findViewById(R.id.TtotalCount);
        mainButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                count+=1;
                current.setText("Your current count is "+ count);
                totalCount = count;
                total.setText("Your total count is " + totalCount);
            }
        });
    }  


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

所以,我有一个整数计数器,每次单击一个按钮时都会增加,但我希望每当用户单击该按钮时,程序会将值保存到其他整数,即使用户关闭了该值,该值也会保留应用

2 个答案:

答案 0 :(得分:0)

如果我理解你需要做的是:

  • 当您单击该按钮时,首先从共享首选项中获取int值,如果不存在则创建它,
  • 将值增加一个
  • 再次将此值保存到SharedPreferences

所以它会转化为类似的东西:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    int totalCount = mySharedPreferences.getInt("INT_KEY", -1);
    if(totalCount == -1) {
       totalCount = 0; //initialize the total count value to 0
    }
    count += 1;
    totalCount += 1;
    SharedPreferences.Editor editor = mySharedPreferences.edit();
    editor.putInt("INT_KEY", totalCount);
    editor.commit();
    current.setText("Your current count is "+ count);
    total.setText("Your total count is " + totalCount);
}

为了提高效率,请在启动应用时从totalCount获取SharedPreferences值,并在完成活动之前将其保存到SharedPreferences

您可以阅读SharedPreferences类的android文档以获取更多信息。

答案 1 :(得分:0)

将以下代码放入按钮的onClick()方法中。

 SharedPreferences sharedPref = getActivity()
                                .getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("variableName", count);
    editor.commit();