使用SharedPrefreferences对象存储数据
来源:互联网 发布:linux打开端口命令 编辑:程序博客网 时间:2024/05/22 07:40
Android为开发人员提供了多种持久化应用 数据的方式,具体选择哪种方式需要具体问题具体分析,例如数据是否仅限于本程序使用,还是可以用于其他程序,以及保存数据所占用的空间等。Android中主要提供 了3种数据存储技术,分别是SharedPreferences、Files和SQLite数据库。
SharedPreferences供开发人员保存和获取基本数据类型的键值对。该类主要用于基本类型,例如booleans、floats、ints、longs和strings。在应用程序结束后,数据依旧对保存。
完成向SharePreferences类中增加值 的步骤如下:
(1)调用SharedPreferences类的edit()方法获得SharedPreferences.Editor对象。
(2)调用诸如putBoolean()、putString()等方法增加值。
(3)使用commit()方法提交新值。
从SharedPreferences类中读取值时,主要使用该类中定义的getXXX()方法。下面以一个简单的例子演示SharedPreferences类的使用。
例1.1 在Eclipse中创建Android项目,使用SharedPreferences保存用户输入的用户名和密码,并在第二个Activity中显示。
代码如下:
SharePreferenceWriteACtivity
package com.example.sharepreferenceswrite;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;public class SharedPreferencesWriteActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shared_preferences_write); final EditText usernameET=(EditText)findViewById(R.id.editText1); final EditText passwordET=(EditText)findViewById(R.id.editText2); Button login = (Button)findViewById(R.id.button1); login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString username = usernameET.getText().toString();//获得用户名String password = passwordET.getText().toString();//获得密码//获得私有类型的SharedPreferencesSharedPreferences sp =getSharedPreferences("mrsoft",MODE_PRIVATE);Editor editor=sp.edit();editor.putString("username", username);//增加用户名editor.putString("password", password);//增加密码editor.commit();//确认提交Intent intent = new Intent();//创建Intent对象//指定跳转到SharedPreferencsReadActivityintent.setClass(SharedPreferencesWriteActivity.this,SharedPreferencesReadActivity.class);startActivity(intent);}}); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.shared_preferences_write, menu); return true; } }
SharedPreferencesReadActivity
package com.example.sharepreferenceswrite;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;import android.widget.TextView;public class SharedPreferencesReadActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_shared_preferences_read);TextView usernameTV = (TextView)findViewById(R.id.textView1);TextView passwoldTV = (TextView)findViewById(R.id.textView2);//获得私有类型的SharedPreferencesSharedPreferences sp = getSharedPreferences("mrsoft",MODE_PRIVATE);String username = sp.getString("username", "mr");//获得用户名String password = sp.getString("password", "001");//获得密码usernameTV.setText("用户名:"+username);passwoldTV.setText("密码:"+password);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.shared_preferences_read, menu);return true;}}
这个例子说明SharedPreferences中的MODE_PRIVATE模式中可以在两个不同的Activity中传输数据。另外还有两种模式:MODE_WORLD_READABLE和MOEE_WORLD_WRITEABLE。它们分别表示对于其他其他应用程序而已,是否可读与可定。
0 0
- 使用SharedPrefreferences对象存储数据
- 使用Files对象存储数据
- [Java] 使用对象存储数据和完成任务
- ASP中使用APPLICATION对象存储数据的缓存类
- 使用Alluxio加速基于Ceph对象存储的数据分析
- 使用对象类存储数据时的错误
- 使用集合存储对象
- 面向对象的数据存储
- QT Internal: 对象数据存储
- 存储数据到共享对象
- 大对象类型数据存储
- 游戏对象数据存储管理器
- QT Internal: 对象数据存储
- HTML5数据存储---使用clear()方法清除localStorage保存对象的全部数据
- HTML5数据存储---使用clear()方法清除localStorage保存对象的全部数据
- HTML5数据存储---使用clear()方法清除localStorage保存对象的全部数据
- 使用组件存储数据
- 使用组件存储数据
- 设计复数类Complex,计算两个复数之和、差,同时以 a+bi 的字符串形式显示
- 二十三个小动作 让你拥有超人般的记忆力
- getFragmentManager()和getSupportFragmentManager()区别
- 余秋雨的话,看到了前15句就不敢往下看了,这才是真正的生活
- mongodb基础
- 使用SharedPrefreferences对象存储数据
- 小猴子下落
- Swift开发工程师职业学习路线图
- 倦眉赋
- Spring 注解@Component,@Service,@Controller,@Repository
- 关于ASIHTTPRequest
- FZU2109(数位dp)
- 第八章-IO库
- 琉璃谣