取SharedPreferences值

取SharedPreferences值

如何从其他活动中读取SharedPreferences值?

当我选择首先开始哪个活动时,我得到了setting.calss,效果很好...

我如何阅读其他活动的偏好设置?

    final SharedPreferences settings = getPreferences(MODE_PRIVATE);
    int choice = settings.getInt("language", -1);


打开Dialog 3活动:

    String[] activities = { "Activity 1", "Activity 2", "Activity 3" };


OnClick:

            @Override
            public void onClick(DialogInterface dialog, int which) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putInt("language", which);
                editor.commit();
                launchActivity(which);
            }
            }).show();
    } else {
        // start the activity and close this activity
        launchActivity(choice);
    }
}

最佳答案

在后面的代码上;

SharedPreferences prefs = this.getSharedPreferences("title",Context.MODE_PRIVATE);
String LanSettining = prefs.getString("language", null);


您还可以使用以下代码:

组:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
                        editor.putString("language", language);
                        editor.commit();


读:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String language = settings.getString("language", "");

关于android - 从另一个 Activity 获取SharedPreferences值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20068855/

10-11 19:22