In Android Application Portrait Mode To Change Landscape Mode My EditText Value is Clear
In A开发者_如何学JAVAndroid Application Portrait Mode To Change Landscape Mode My EditText Value is Clear
When there is rotation happened activity always restarts.
so keep this in mind, persist the variables while rotating from portrait to landscape and vice versa.
EditText should automagically save the text value on orientation change. I can think of two reasons why it would be cleared:
1) you are implementing onSaveInstanceState, but failing to call super.onSaveInstanceState 2) the editText does not have a valid ID. An example of an editText without a valid ID is a programmatically created editText such as adding an editText to an AlertDialog. This can be solved by creating a simple layout as in:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:text="Stateful"
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
And inflating the layout as in:
AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
builder.setTitle("About");
builder.setMessage(alertMessage+"Version: "+versionName);
builder.setView(myView);
AlertDialog alert= builder.create();
Hope that helps.
精彩评论