简体   繁体   English

Android偏好设置:使用编辑文本时出现问题

[英]Android Preferences : issue while using an edit text

I'm new to android,I have an edit text and I wanted to save the text entered on it through preferences.Can any one help me out.... 我是android新手,我有一个编辑文本,我想保存通过首选项输入的文本。任何人都可以帮帮我...

Thanks inadvance 提前致谢

There are actually 3 ways to persist data in android: 实际上,有3种方法可以将数据持久保存在android中:

  1. Shared Preferences 共享首选项
  2. Local Files (xml, csv, etc) 本地文件(xml,csv等)
  3. Sql Lite Database SQL Lite数据库

Here is some code to get you started with "Shared Preferences" 这是一些代码,可帮助您开始使用“共享的首选项”

//get reference to a Shared Preferences instance
SharedPreferences preferences = context.getSharedPreferences("Some Name You Make Up", 0);

//add a string to the given Shared Preferences store by key
SharedPreferences.Editor editor = preferences.edit();
editor.putString("some_key", "some_value");

//read the value from the Shared preferences
String value = preferences.getString("some_key", " ");

Here is the link to the android documentation on shared preferences. 这是指向共享首选项的android文档的链接。

http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/reference/android/content/SharedPreferences.html

Enjoy! 请享用!

 SharedPreferences settings=getSharedPreferences("mypref", 0);
 et.setText(settings.getString("tvalue"," "));

Then override onstop() method 然后重写onstop()方法

@Override @覆盖

protected void onStop() {
     // TODO Auto-generated method stub
    super.onStop();
    SharedPreferences settings=getSharedPreferences("mypref", 0);
    SharedPreferences.Editor editor=settings.edit();
    editor.putString("tvalue", et.getText().toString());
    editor.commit();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM