Java 程序关闭后保存多个编辑文本
我想为一个类似DnD的游戏制作一个角色表。关键是获取用户输入并存储,以备将来查看和更改。也许我在寻找一个不存在的解决方案,但如果用户关闭应用程序或只是返回标题屏幕,我希望编辑文本保持原样。下面是用于用户输入的xml文件的一部分,java文件基本上是空白的Java 程序关闭后保存多个编辑文本,java,android,xml,Java,Android,Xml,我想为一个类似DnD的游戏制作一个角色表。关键是获取用户输入并存储,以备将来查看和更改。也许我在寻找一个不存在的解决方案,但如果用户关闭应用程序或只是返回标题屏幕,我希望编辑文本保持原样。下面是用于用户输入的xml文件的一部分,java文件基本上是空白的 **<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
**<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/scroll_s">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:inputType="textPersonName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/player"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/player"
android:inputType="textPersonName"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pts_total"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unspent_pts"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/pts_total"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/unspent_pts"
android:layout_weight="1"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ht"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/wt"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/age"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/height"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/weightt"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/age"
android:inputType="number"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/size_mod"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/appearance"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appearance"
android:inputType="text"/>
</LinearLayout>
</ScrollView>**
以下是一个例子:
public class Character_activity extends AppCompatActivity {
private EditText playerEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
playerEditText = (EditText) findViewById(R.id.player);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
playerEditText.setText(savedInstanceState.getString("SavedPlayer", "");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("SavedPlayer", playerEditText.getText().toString());
super.onSaveInstanceState(outState);
}
}
这将通过方向更改保存文本。如果您想在进程终止时保存它,那么您需要查看SharedReferences
,而不是Bundle
谢谢!你的答案和这个一起帮助我解决了这个问题!
public class Character_activity extends AppCompatActivity {
private EditText playerEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
playerEditText = (EditText) findViewById(R.id.player);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
playerEditText.setText(savedInstanceState.getString("SavedPlayer", "");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("SavedPlayer", playerEditText.getText().toString());
super.onSaveInstanceState(outState);
}
}