1

我创建了一个从strings.xmlwith中获取 StringSharedPreferences并将其显示在TextView. 没有错误报告,但没有TextView显示任何内容。有问题SharedPreferences吗?字符串和TextView是正确的。

public void setQuestion() {
    TextView Question = (TextView) findViewById(R.id.question);
    if (question == 0) {
        SharedPreferences sharedPreferences =
                getSharedPreferences("strings", Context.MODE_PRIVATE);
        String myquestion = sharedPreferences.getString("AppQuestion1", "");
        Question.setText(myquestion);
    }
}
4

1 回答 1

1

strings.xmlSharedPreferences是不同的东西。

如果您AppQuestion1的定义strings.xml如下:

<string name="AppQuestion1">Question1: ...</string>

您可以通过在对象getString上调用方法来获取字符串。Resources

String myquestion = getResources().getString(R.string.AppQuestion1);

并且没有必要使用SharedPreferences.

于 2015-09-16T20:31:19.690 回答