程序关闭后保存多个EditText

时间:2015-11-26 20:20:40

标签: java android xml

我试图为类似DnD的游戏制作角色表。重点是获取用户输入并存储它以供将来查看和更改。也许我正在寻找一个不存在的解决方案,但我想让EditTexts保持原样,如果用户关闭应用程序或只是回到标题屏幕。以下是用户输入的xml文件的一部分,java文件基本上是空白的

**<?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>**

Java文件

package com.example.mapuchii.gurps;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Character_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_redo);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Read values from the "savedInstanceState"-object and put them in your textview
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save the values you need from your textview into "outState"-object
    super.onSaveInstanceState(outState);
}
}

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

casper.then(function readFile() {
        var stream = fs.open('ck.csv', 'r');
        var line = stream.readLine();
        var lineArray = [];
        while(line) {
            lineArray.push(line);
            line = stream.readLine();
        }

    casper.eachThen(lineArray, function(response) {
        line = response.data.split(',');

        this.echo(line[0] + ' - ' + email);

        if(line[0] === email && line[1] === 'New'){

这会将文字保存在方向更改上。如果您希望在流程被终止时保存它,那么您需要查看SetEnvIf Remote_Addr ^127\.0\.0\.1 islocal CustomLog logs/access_log common env=!islocal CustomLog logs/access_log_local common env=islocal 而不是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); } }

http://developer.android.com/training/basics/data-storage/shared-preferences.html