【问题标题】:Saving several EditText after program closes程序关闭后保存几个 EditText
【发布时间】:2015-11-26 20:20:39
【问题描述】:

我正在尝试为类似 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);
}
}

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    这是一个例子:

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

    这将在方向更改时保存您的文本。如果您想在进程被终止时保存它,那么您需要查看SharedPreferences 而不是Bundle

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多