【问题标题】:Saving data from Text Field从文本字段保存数据
【发布时间】:2014-03-19 03:10:03
【问题描述】:

我的应用程序有一些可编辑的文本字段,但没有保存他添加的内容,我想退出应用程序,并且在重新输入我的数据时仍然存在。

XML

 <DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="28dp"
    android:layout_marginTop="28dp"
    android:text="DigitalClock"
    android:textColor="#FA8072"
    android:textSize="40dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/digitalClock1"
    android:layout_toLeftOf="@+id/digitalClock1"
    android:text="MEDICINES"
    android:textColor="#228B22"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="20dp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="39dp"
    android:ems="10" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="Name" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="8dp"
    android:text="Name" />

<EditText
    android:id="@+id/EditText01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView01"
    android:layout_centerVertical="true"
    android:layout_marginTop="8dp"
    android:ems="10" />

</RelativeLayout>

JAVA

    public PagesFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_pages, container, false);

    return rootView;
}
}

【问题讨论】:

  • 要清除之前输入的数据吗?
  • 所以你的意思是当你的应用被用户杀死时你的数据应该是持久的???如果是,那么您必须将您的 editttext 值存储在 sharedPrefrences 中。
  • 不,例如我有我的文本字段,我想添加我的姓名和年龄,我输入我的姓名和年龄然后我离开应用程序,然后返回应用程序和我的名字并且年龄必须保存在应用程序中。
  • 我是怎么做到的?是我的问题吗...因为我是新编程!
  • @mcd 我可以这样做吗?

标签: java android save textfield android-xml


【解决方案1】:
    //For storing string value in sharedPreference     
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("Name","Harneet");
    editor.commit();

    //For retrieving string value from sharedPreference
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String name = preferences.getString("Name","");

【讨论】:

    【解决方案2】:

    您可以使用 bundle 将 edittext 值存储在 onSaveInstanceState 上。并在 oncreate 方法中检查条件,如果 bundle 对象不为空,则它是否为空,然后使用 get* 方法从中检索。如果您想在整个应用程序中以持久的方式存储数据,您可以使用 sharedprefresce 。

    http://developer.android.com/training/basics/activity-lifecycle/recreating.html

    【讨论】:

      【解决方案3】:

      您可以尝试使用SharedPreference。在您的活动中覆盖 onPause() 并将文本存储在 SharedPreference 中。在活动的 onResume() 中,从 SharedPreference 中检索它并将其加载回您的 EditText

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-16
        • 1970-01-01
        • 2023-03-14
        • 2019-08-23
        • 1970-01-01
        • 2014-05-01
        相关资源
        最近更新 更多