java - 如何在共享首选项中保存 TextView ?

标签 java android eclipse textview

我希望我的应用程序将 textView2 保存在共享首选项中。这是我当前的共享首选项代码,用于保存复选框的状态(选中/未选中)。

private boolean getFromSP(String key){
SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
return bifrostPrefs.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = bifrostPrefs.edit();
editor.putBoolean(key, value);
editor.commit();
}

我以前从未真正使用过共享首选项,所以我真的不知道如何创建一个新的共享首选项来保存我的 textview2。

最佳答案

你不能。

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.

来自文档 Using Shared Preferences

Al-thought你可以存储TextView的值

//To get stored value
    private String getString(String key){
        SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
        return bifrostPrefs.getString(key, "");
    }

..

//To Save value
    private void saveString(String key, String value){
         SharedPreferences bifrostPrefs = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
         SharedPreferences.Editor editor = bifrostPrefs.edit();
         editor.putString(key, value);
         editor.commit();
    }
<小时/>

如何使用这些方法

将此代码放在要保存 TextVIew 值的位置

//To save value of TextView
if (!TextUtils.isEmpty(aTextView.getText())) {
    saveString("aTextView", aTextView.getText().toString());
}

//To Read and show into TextVIew
aTextView.setText(getString("aTextView"));

关于java - 如何在共享首选项中保存 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17086234/

相关文章:

android - 无法将 android.support.v7 添加到我的项目构建路径

java - UIAutomator 在 Linux 上构建时运行 0 个测试,但在其他平台上运行良好

java - 单击按钮时在 Java 中设置标签

Java加密等于不起作用

Android Studio 属性窗口丢失

java - Google Glass Live Card 菜单操作导致崩溃

android - 声明为不可为 null 的 Kotlin 属性即使已初始化值也可以为 null

eclipse - 如何让 Eclipse 识别 Java EE jar 文件以便 Servlet 可以编译?

java - Spring UriTemplate 线程安全吗?

java - 如何将 Solr max 函数应用于所有字段