如何改变活动时保持一个TextView的内容 [英] How to keep a TextView's content when changing the Activity

查看:131
本文介绍了如何改变活动时保持一个TextView的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的Andr​​oid开发,我已经遇到一个问题,我的TextView。我有一个包含滚动型和一个TextView的XML文件:
    

I'm pretty new to Android Development and I've come across a problem with my TextView. I have an XML file that contains a ScrollView and a TextView:

<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文件中包含它

And I have included it in two different XML files

<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文件转到另一个(和改变的过程中的活动),则TextView的内容会消失。我曾尝试freezesText但似乎并没有工作。

But when I go from the first XML file to the other (and changing the Activity in the process), the content of the TextView disappears. I have tried freezesText but that doesn't seem to work.

通常我只想传递一个意图内容,但是我的文字是不同的颜色和我要维护。

Ordinarily I would just pass the content in an intent but my text is in different colours and I want to maintain that.

我可以传递一个意图位图图像,但我想避免,如果可能的。

I could pass a Bitmap image in an intent but I want to avoid that if possible.

感谢。

推荐答案

这发生,因为当你回来的第一项活动,onCreate方法被调用,所以你的TextView是一个新的TextView。
看看到活动生命周期,这里解释好得多。

That happen because when you come back to the first activity, the onCreate method is called, so your textView is a new textView. Take a look to Activity Lifecycle, here is explained much better.

您可以使用 共享preferences 辛格尔顿 (设计模式),其中类可仅实例化一次:

You can use sharedPreferences or a Singleton (a design pattern), where the class can be instantiate only one time:

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");
   }

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

Maybe this way it's longer than the shared preferences, but it's very useful. Excuse my bad english! I hope this help.

这篇关于如何改变活动时保持一个TextView的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆