3

我在堆栈溢出中阅读了几乎所有关于 onSaveInstanceState 和 onRestoreInstanceState 的文章,但我无法解决我的问题。
我的 main.java 中有一个文本视图和按钮,当您单击一个(a是一个 int 变量)变量的按钮值时,变量将增加并显示在文本视图中,但是,当我旋转我的手机(方向更改)时,文本视图重置。

我覆盖 onSaveInstanceState 和 onRestoreInstanceState 但它不起作用。还有一件事,我有用于横向视图的特殊 layout-land.xml 文件。

这是我的代码

 import android.app.Activity; 
 import android.graphics.Typeface; 
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

 public class main extends Activity {
     /** Called when the activity is first created. */   
   public int a = 0;     
   public String fonts="TAHOMA.TTF";

   TextView tv = (TextView) findViewById(R.id.salavat);      
   Button b = (Button) findViewById(R.id.showsalavat);

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       b.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
               a++;
               tv.setText(""+a);
            }
       });
   }
   @Override        
   protected void onSaveInstanceState(Bundle SavedInstanceState) {
      super.onSaveInstanceState(SavedInstanceState);            
      SavedInstanceState.putInt("salavat-count", a);
   }
   @Override    
   protected void onRestoreInstanceState(Bundle savedInstanceState) {
       super.onRestoreInstanceState(savedInstanceState);     
       a= savedInstanceState.getInt ("salavat-count");   
   } 
}

这里是我的 main.xml

    <TextView
        android:id="@+id/shoma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/shoma"
        android:textSize="20sp" />

    <Button
        android:id="@+id/showsalavat"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:text="+"
        android:textSize="100sp" />

    <TextView
        android:id="@+id/eltemas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="55dp"
        android:text="@string/eltemas" />

    <TextView
        android:id="@+id/salavat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="35sp" />

    <TextView
        android:id="@+id/ferestade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:text="@string/salavat"
        android:textSize="20sp" />

</LinearLayout>

我真的需要一些帮助。我真的很感谢你的帮助。

4

5 回答 5

7

作为更新,您可以设置freezesText="true"

http://developer.android.com/reference/android/R.attr.html#freezesText

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

或者


这是最简单的例子:

TextView yourTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    yourTextView = (TextView) findViewById(R.id.your_textview);

}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("YourTextViewTextIdentifier", yourTextView.getText().toString());

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        yourTextView.setText(savedInstanceState.getString("YourTextViewTextIdentifier"));
    }

但是,当您开始在方向更改时保存大量对象时,拥有一个表示您的状态的内部类会更好。

使用内部类,它看起来像下面这样。您可以想象,当您拥有越来越多的状态来保存内部类时,处理起来会变得更加容易(不那么混乱)。

  private static class State implements Serializable {

    private static final String STATE = "com.your.package.classname.STATE";

    private String yourTextViewText;

    public State(String yourTextViewText) {
        this.yourTextViewText = yourTextViewText;
    }

    public String getYourTextViewText() {
        return yourTextViewText;
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    State s = new State(yourTextView.getText().toString());

    outState.putSerializable(State.STATE, s);

    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    State s = (State) savedInstanceState.getSerializable(State.STATE);

    yourTextView.setText(s.getYourTextViewText());
}
于 2012-04-21T14:34:52.157 回答
5

我认为这段代码对你有帮助..

public class MainActivity extends Activity 
{

    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        System.out.println("TAG, onSavedInstanceState");

        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = text.getText();
        outState.putCharSequence("savedText", userText);
    }
    protected void onRestoreInstanceState(Bundle savedState) 
    {       
        System.out.println("TAG, onRestoreInstanceState");
        final TextView text = (TextView)findViewById(R.id.textView1);
        CharSequence userText = savedState.getCharSequence("savedText");
        text.setText(userText);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String name = "5";

        final TextView show = (TextView)findViewById(R.id.textView1);
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() 
        {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                show.setText(name);

            }
        });
    }
}
于 2012-11-23T12:43:56.463 回答
0

您需要使用保存的值重建视图组件。用保存的值填充您的文本视图。

@Override   
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  a = savedInstanceState.getInt ("salavat-count");
  tv.setText(""+a);
}
于 2012-04-21T14:35:48.847 回答
0

你真的要使用这些方法吗?

在这里,您可以找到一种解决问题的方法,只需将其放在清单文件中

<activity android:name="package.subpackage.xptoactivity" android:configChanges="orientation"></activity>

如果你这样做,当你改变方向时,你的 Activity 不会重新加载所有视图来重置它们的值

于 2012-04-21T19:10:33.537 回答
0

一个被忽视但严重的错误。您必须首先将数据传递给 onSavedInstanceState() 并调用 onSavedInstanceState() 。如果您认为那么您会发现在传递要保存的数据之前调用 onSavedInstanceState 是没有用的。它不会保存活动实例,因为之前调用了 onSavedInstanceState ()。它应该是:

@Override
protected void onSaveInstanceState(Bundle SavedInstanceState) 
{           
    SavedInstanceState.putInt("salavat-count", a);
    super.onSaveInstanceState(SavedInstanceState); 
}
于 2014-06-27T20:18:10.650 回答