【问题标题】:How to save textview when application is closed?关闭应用程序时如何保存文本视图?
【发布时间】:2019-11-26 02:38:08
【问题描述】:

我正在尝试从 getIntent 字符串中保存 textview。当我关闭应用程序,然后再次打开时,textview 为空。我用过很多方法,比如onSaveInstanceSharedPreference,但都失败了。

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    jamShubuh = findViewById(R.id.jamShubuhTextView);
    jamDzuhur = findViewById(R.id.jamDzuhurTextView);
    jamAshar = findViewById(R.id.jamAsharTextView);

    getShubuh = getIntent().getStringExtra("getShubuh");
    getDzuhur = getIntent().getStringExtra("getDzuhur");
    getAshar = getIntent().getStringExtra("getAshar");

    jamShubuh.setText(getShubuh);
    jamDzuhur.setText(getDzuhur);
    jamAshar.setText(getAshar);

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("shubuh", getShubuh);
    editor.putString("dzuhur", getDzuhur);
    editor.putString("ashar", getAshar);

    retriveData();

private void retriveData() {
    SharedPreferences getData = getPreferences(Context.MODE_PRIVATE);
    getData.getString("shubuh", null);
    getData.getString("dzuhur", null);
    getData.getString("ashar", null);
}

【问题讨论】:

  • 这不是minimal, reproducible example,您显示了一些不显示 R 声明的随机代码。换句话说,您使用了一个未显示的名为 R 的变量,这可能导致上述错误。另外,您能否包含一条错误消息,其中包含指向您描述的导致问题的行的指针?
  • edit 向我们展示您对SharedPreferences 的尝试。这似乎是只保留三个Strings 的合适选择。
  • @KnowNoTrend R 是 Android 中自动生成的资源类。这只是一堆final static 字段。通常不需要在问题中包含它。
  • @MikeM。等你
  • @MikeM。问题已编辑

标签: java android textview sharedpreferences onsaveinstancestate


【解决方案1】:

要使用共享首选项,我建议您创建某些类来处理它。也许是这样的。

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class MyPrefManager {
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    public void setPref(Context context, String key, String value){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public String getPref(Context context, String key){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        value = null;
        String X = sp.getString(key, value);
        return X;
    }

}

然后使用 setPref 方法设置值和 getPref 方法获取值。假设现在您在 MainActivity 中,并且您想将 getShubuh、getDzuhur 和 getAshar 的值保存到 Preference Manager,您可以执行以下操作:

MyPrefManager MPM = new MyPrefManager();

MPM.setPref(MainActivity.this,"keyShubuh",getShubuh);
MPM.setPref(MainActivity.this,"keyDzuhur",getDzuhur);
MPM.setPref(MainActivity.this,"keyAshar",getAshar);

要在任意位置获取 keyShubuh、keyDzuhur 和 keyAshar 的值,您可以执行以下操作。

MyPrefManager MPM = new MyPrefManager();

MPM.getPref(AnyActivity.this,"keyShubuh");
MPM.getPref(AnyActivity.this,"keyDzuhur");
MPM.getPref(AnyActivity.this,"keyAshar");

希望对你有帮助。

【讨论】:

  • 达里印度尼西亚 ya? Saya mau textview.setText(); di save tanpa mencet tombol apapun karena value yang saya dapet hasil dari getIntent().getStringExtra(); dan saya tidak mau ada tombol sama sekali di xml nya。 Masalahnya ketika saya tutup aplikasinya trus saya buka lagi, value yang ada di textview berubah jadi null。
  • Iya mas,simpan saja dulu di 共享偏好,lalu 加载 valuenya setiap awal load projectnya。 Kalo pengen ga ada tombol,berarti buat addTextChangedListener pada EditText nya。 Nanti akan muncul 3 方法,gunakan 方法 onTextChanged untuk 设置值 nya.
【解决方案2】:

保存文本状态

//Saving The Text Entered by User
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    editText = findViewById(R.id.editText);// getting the reference of editext from xml
    CharSequence text = editText.getText();// getting text u entered in edittext
    outState.putCharSequence("savedText", text);// saved that text in bundle object i.e. outState
}

并恢复保存的文本

//Restoring the State
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    editText = findViewById(R.id.editText);// getting the reference of textview from xml

    CharSequence savedText =
            savedInstanceState.getCharSequence("savedText");// getting the text of editext

    editText.setText(savedText);// set the text that is retrieved from bundle object
}

您可以使用 TextView 代替 EditText。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2014-08-06
    • 2011-10-04
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多