使用共享首选项存储int值 [英] Storing an int value using Shared preferences

查看:54
本文介绍了使用共享首选项存储int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int"flag"变量,它将有两个可能的int值:0&1.

I have an int "flag" variable , which will have two possible int values , 0 & 1.

应用程序的主要主题是:询问用户是还是否?

Main theme of the app here is : Ask user yes or no?

如果用户选择是,则分配int = 1.否则,如果选择否",则分配int = 0;

If user selects YES, assign int=1. else if Selects No, assign int=0;

我正在使用以下方法实现这一目标:

I am achieving this using:

public static int flag;


    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.flag0:
        flag=0;
        System.err.println("Flag : " + flag);
        break;
    case R.id.flag1:
        flag=1;
        System.err.println("Flag : " + flag);
        break;
    default:
        break;
    }
}

我的问题在这里:没关系&将首选项保存到我的应用程序正在前台运行还是在后台运行.

My problem is here: it is ok & saving the preference upto my Application is running either foreground or background.

并且一旦我们关闭/杀死该应用程序,就会清除所有存储的偏好设置,即设置为默认值零.

and once if we close/kill the application, all the stored preferences are being cleared i.e., setting to the default value of Zero.

当我刚开始使用STORAGE Options时,我只是试图实现SHARED PREFERENCES,但仍然存在上述相同的问题.我使用共享"首选项存储int值的方法是;

As I just started working on STORAGE Options, I just tried to implement SHARED PREFERENCES, which still having the same above problem . the method I used to store the int value using Shared preferences is ;

我只是尝试了以下答案,但没有成功&这是我要发布的全部代码,将有助于您测试(也许)

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/text"
    android:textSize="30sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button android:id="@+id/flag0"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:text="set to 0"/>

<Button android:id="@+id/flag1"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/text"
    android:layout_toRightOf="@+id/flag0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="set to 1"/>

<Button android:id="@+id/test0"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/flag1"
    android:text="Test for 0"/>

MainActivity:

MainActivity:

public class MainActivity extends Activity implements OnClickListener {
TextView textView;
Button bt1,bt2,testbt1;
public static int flag;
SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.text);
    bt1 = (Button) findViewById(R.id.flag0);
    bt2 = (Button) findViewById(R.id.flag1);
    testbt1 = (Button) findViewById(R.id.test0);

    bt1.setOnClickListener(this);
    bt2.setOnClickListener(this);
    testbt1.setOnClickListener(this);


    pref=getSharedPreferences("Sai", MODE_PRIVATE);
    pref.getInt("lang_us", 0);
    System.err.println("Flag Value in Main method : " + flag);
}
public void saveFlag(){

    pref=getSharedPreferences("Sai",MODE_PRIVATE);
    Editor editor = pref.edit();
    editor.putInt("lang_us", flag);
    editor.commit();

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.flag0:
        flag=0;
        saveFlag();
        System.err.println("Flag : " + flag);
        break;
    case R.id.flag1:
        flag=1;
        saveFlag();
        System.err.println("Flag : " + flag);
        break;
    case R.id.test0:
        System.err.println("Flag : " + flag);
        if (flag == 0) {
            textView.setText("flag is zero");
        } else {
            textView.setText("flag is not zero");
        }

        break;

    default:
        break;
    }
}

}

哪里出问题了?

推荐答案

您的处理方式有误

1.>使用Mode_PRIVATE(当您希望共享的首选项文件只能由您的应用程序使用时使用)而不是MODE_WORLD_WRITABLE(当您希望共享的首选项文件仅应在应用程序外部访问时使用)

1.> Use Mode_PRIVATE(Used when you want your shared preference file should be accessible only to your app) instead of MODE_WORLD_WRITABLE(Used when you want your shared preference file should be accessible only outside your app)

2.>制定一种保存标志状态的方法

2.> Make a method to save the state of flag

public void saveFlag(){

    pref=getSharedPreferences("Sai",Context.MODE_PRIVATE);
    Editor editor = pref.edit();
    editor.putInt("lang_us", flag);
    editor.commit();

}

3.>每当更改标志时都调用此方法

3.> Call this method whenever you change flag

     @Override
     public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.flag0:
    flag=0;
    saveFlag();
    System.err.println("Flag : " + flag);
    break;
case R.id.flag1:
    flag=1;
    saveFlag();
    System.err.println("Flag : " + flag);
    break;
default:
    break;
}

4.>然后,应在onCreate()方法中检索该值.

4.> Then in your onCreate() method you should retrieve this value.

pref=getSharedPreferences("Sai",Context.MODE_PRIVATE);
flag  = prefs.getInt("lang_us", 0); 

这篇关于使用共享首选项存储int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆