如何在更改活动时保留TextView的内容

时间:2014-07-03 13:52:35

标签: android xml android-layout textview

我是Android开发的新手,我遇到了TextView的问题。我有一个包含ScrollView和TextView的XML文件:     

<ScrollView
    android:id="@+id/scroll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

            <TextView 
                android:id="@+id/textView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:freezesText="true">
            </TextView>

    </ScrollView>

我已将其包含在两个不同的XML文件中

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <include
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/story_view"
        layout="@layout/story_view" />

    <EditText 
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/add_text">
    </EditText>

</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button android:text="@string/end_button"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:onClick="endButtonPressed">
    </Button>

    <Button android:text="@string/submit_button"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:onClick="textAdded">
    </Button>

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/story_view" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button android:text="@string/save"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:onClick="saveToDevice">
    </Button>

    <Button android:text="@string/facebook"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:onClick="saveToFacebook">
    </Button>

</LinearLayout>

但是当我从第一个XML文件转到另一个文件(并在此过程中更改Activity)时,TextView的内容消失了。我尝试过freezesText,但似乎没有用。

通常我会在意图中传递内容,但我的文字颜色不同,我想保留它。

我可以在意图中传递一个Bitmap图像,但我希望尽可能避免这种情况。

感谢。

3 个答案:

答案 0 :(得分:3)

您可以使用“活动”状态保存某些值,例如

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    savedInstanceState.putBoolean("MyBoolean", true);
    savedInstanceState.putDouble("myDouble", 1.9);
    savedInstanceState.putInt("MyInt", 1);
    savedInstanceState.putString("MyString", "Welcome back to Android");
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
    double myDouble = savedInstanceState.getDouble("myDouble");
    int myInt = savedInstanceState.getInt("MyInt");
    String myString = savedInstanceState.getString("MyString");
}

答案 1 :(得分:0)

您可以使用sharedpreferences来实现此目的,它允许您使用key-value pairs存储和检索数据。为了满足您的目的,您需要将活动状态存储在sharedpreferences

特别是在你的情况下 首先,您需要在销毁活动时将textview值存储在共享首选项中

@Override
        public void onDestroy() {
            super.onDestroy();
TextView tvText = (TextView) findViewById(R.id.yourelement);
            SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
            prefEditor.putBoolean("text", tvText .getText().toString());
            prefEditor.commit();
        }

然后在onCreate中,您需要设置共享首选项中的textview文本。

TextView tvText = (TextView) findViewById(R.id.yourelement);
    SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
    if (prefs.contains("text")){
            tvText .setText(prefs.getString("text", ""));
        }

答案 2 :(得分:0)

之所以发生这种情况,是因为当您回到第一个活动时,会调用onCreate方法,因此您的textView是一个新的textView。 看看Activity Lifecycle,这里解释得更好。

您可以使用sharedPreferencesSingleton(设计模式),其中类只能实例化一次:

public class MySingleton{

    private static MySingleton INSTANCE = null;
    private String textViewInformation;

    private MySingleton() {

    }

    public static MySingleton getInstance(Context context) {
        synchronized (MySingleton.class) {
            if (INSTANCE == null) {
                synchronized (MySingleton.class) {
                    if (INSTANCE == null)
                        INSTANCE = new MySingleton();
                }
            }
        }
        return INSTANCE;
    }

  public String getTextViewInformation(){
    return textViewInformation;
  }

 public void setTextViewInformation(String textViewInfo){
  textViewInformation = textViewInfo;
 }

}

然后:

 public void onDestroy() {
            super.onDestroy();
      MySingleton.getInstance(this).setTextViewInformation("textViewText");
     }



  public void onResume() {
            super.onResume();

   if(MySingleton.getInstance(this).getTextViewInformation() != null){
     yourTextView.setText(MySingleton.getInstance(this).getTextViewInformation());
   }else{
     yourTextView.setText("new text");
   }

也许这种方式比共享偏好更长,但它非常有用。 请原谅我糟糕的英语! 我希望这有帮助。