首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在SharedPreferences保存int时遇到了问题

我在SharedPreferences保存int时遇到了问题
EN

Stack Overflow用户
提问于 2015-07-19 04:48:03
回答 1查看 65关注 0票数 0
代码语言:javascript
复制
package com.carterharrison.taptap;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    SharedPreferences SP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SP = getSharedPreferences("points", Context.MODE_PRIVATE);


    }




        public int x = SP.getInt("points", -1);



    public void val1(View v){

        x = (x+1);
        con();

    }

    public void val2(View v){

        x = (x+2);
        con();
    }

    public void val3(View v){

        x = (x+5);
        con();
        }

    public void val4(View v){

        x = (x+10);
    con();

}

public void val5(View v){

    x = (x+25);
    con();

}

public void val6(View v){

    x = (x+50);
    con();

}

public void con(){
    SharedPreferences.Editor editor  = SP.edit();

    if (x > 1000) {
        ImageView img5=(ImageView)findViewById(R.id.imageView5);
        img5.setVisibility(View.VISIBLE);}
    if (x > 625) {
        ImageView img4=(ImageView)findViewById(R.id.imageView4);
        img4.setVisibility(View.VISIBLE);}
    if (x > 300) {
        ImageView img3=(ImageView)findViewById(R.id.imageView3);
        img3.setVisibility(View.VISIBLE);}
    if (x > 100) {
        ImageView img2=(ImageView)findViewById(R.id.imageView2);
        img2.setVisibility(View.VISIBLE);}
    if (x > 30) {
        ImageView img=(ImageView)findViewById(R.id.ImageView);
        img.setVisibility(View.VISIBLE);}

    TextView tv = (TextView)findViewById(R.id.val);
    tv.setText("" + x);


    editor.putInt("score", x);
    editor.apply();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我正在努力保存int的价值。我的应用程序在启动时崩溃了,有人能帮我吗?我对java和SharedPreferences是新手,谢谢。我可能做错了,我需要帮助保存x.的值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-19 05:29:23

SharedPReference.中设置int

代码语言:javascript
复制
public void SaveInt(String key, int value){
    SharedPreferences sharedPreferences = getSharedPreferences("points", Activity.MODE_PRIVATE)
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
} 

SharedPreference获取Int

代码语言:javascript
复制
public void LoadInt(String key){
    SharedPreferences sharedPreferences = getSharedPreferences("points", Activity.MODE_PRIVATE)
    int savedValue = sharedPreferences.getInt(key, 0);
}

编辑:

每当您想要在中保存、int、值时,共享首选项只需调用以下方法:

SaveInt("my_int",5);

每当您想检索相同int的值时:

int mScore = LoadInt("my_int");

希望它能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31498090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档