从零开始学android<数据存储(1)SharedPreferences属性文件.三十五.>
2014-08-27 22:35
621 查看
在android中有五种保存数据的方法,分别是:
Shared Preferences
Store private primitive data in key-value pairs.
对应属性的键值对属性文件存储
Internal Storage
Store private data on the device memory.
设备内存存储
External Storage
Store public data on the shared external storage.
外部存储器存储,如内存卡
SQLite Databases
Store structured data in a private database.
sqlite数据库存储
Network Connection
Store data on the web with your own network server.网络存储
今天这一节我们一起来学习Shared
Preferences 属性文件存储的方式来存储简单的数据
我们可以使用Shared
Preferences 来存储 booleans, floats, ints, longs, and strings型的简单数据并以键值对的形式保存为xml文件。
为了实例化Shared
Preferences 我们可以使用
getSharedPreferences()和
第一个方法需要传入一个文件名和存储的模式
第二种方法默认为只有一个属性文件,只需要传入一个存储模式就行了
存储模式 :
MODE_PRIVATE仅本应用可用
具体操作见代码注释
xml文件
JAVA文件
接下来使用改方法来实现本地记住账号和密码的功能
里面可能会涉及到一些没有讲到的知识,大家可以先了解下,也对前面学过的其他组件进行一下回顾
xml文件
主界面
登录后界面
JAVA文件
登陆后的界面
未点击记住密码,第二次登录。
点击记住密码登录
点击记住密码后,第三次登录
介绍了SharedPreferences属性文件的存储,我们可以进行较小数据的快速存储与便捷读取
下节预报:Internal
Storage内部存储器
Shared Preferences
Store private primitive data in key-value pairs.
对应属性的键值对属性文件存储
Internal Storage
Store private data on the device memory.
设备内存存储
External Storage
Store public data on the shared external storage.
外部存储器存储,如内存卡
SQLite Databases
Store structured data in a private database.
sqlite数据库存储
Network Connection
Store data on the web with your own network server.网络存储
今天这一节我们一起来学习Shared
Preferences 属性文件存储的方式来存储简单的数据
我们可以使用Shared
Preferences 来存储 booleans, floats, ints, longs, and strings型的简单数据并以键值对的形式保存为xml文件。
为了实例化Shared
Preferences 我们可以使用
getSharedPreferences()和
getPreferences()这两个方法
第一个方法需要传入一个文件名和存储的模式
第二种方法默认为只有一个属性文件,只需要传入一个存储模式就行了
存储模式 :
MODE_PRIVATE仅本应用可用
MODE_APPEND可追加
MODE_WORLD_READABLE,可被其他应用读
MODE_WORLD_WRITEABLE.可被其他应用写
具体操作见代码注释
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="89dp" android:text="存储信息" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="36dp" android:text="读取信息" /> </RelativeLayout>
JAVA文件
package com.example.sharedpreferences; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private SharedPreferences sharedPreferences; private Button saveData, getDate; public static final String FILENAME = "flyou"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedPreferences = getSharedPreferences(FILENAME, MODE_PRIVATE); saveData = (Button) this.findViewById(R.id.button1); getDate = (Button) this.findViewById(R.id.button2); saveData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username", "jay"); editor.putString("password", "553274238"); Boolean flag = editor.commit(); Toast.makeText(MainActivity.this, "执行完成,执行结果:-->" + flag, 2) .show(); } }); getDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String username = sharedPreferences.getString("username", "未找到匹配信息"); String password = sharedPreferences.getString("password", "未找到用户密码"); Toast.makeText(MainActivity.this, "用户名:——>" + username + ",密码:——>" + password, 2).show(); } }); } }
接下来使用改方法来实现本地记住账号和密码的功能
里面可能会涉及到一些没有讲到的知识,大家可以先了解下,也对前面学过的其他组件进行一下回顾
xml文件
主界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="19dp" android:layout_marginTop="42dp" android:text="用户名" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="58dp" android:text="密 码" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="40dp" android:layout_toRightOf="@+id/textView1" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_alignLeft="@+id/editText1" android:ems="10" android:inputType="textPassword" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_alignRight="@+id/editText2" android:text="记住密码" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignParentLeft="true" android:text="注册账号" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/checkBox1" android:text="登录" /> </RelativeLayout>
登录后界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
JAVA文件
package com.example.sharepreferencesdemo; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class MainActivity extends Activity { private SharedPreferences sharedPreferences; private Button login; private CheckBox checkBox; private EditText username, password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); login = (Button) this.findViewById(R.id.button1); checkBox = (CheckBox) this.findViewById(R.id.checkBox1); username = (EditText) this.findViewById(R.id.editText1); password = (EditText) this.findViewById(R.id.editText2); sharedPreferences = getPreferences(MODE_PRIVATE);// 通过getPreferences实例化sharedPreferences对象 String usernameString = sharedPreferences.getString("username", "");// 读取用户名 username.setText(usernameString);// 为编辑框设置内容 // 获取复选框的选中状态,如果选中的话就 进行记住密码的操作 if (sharedPreferences.getBoolean("checked", false)) { // 获取密码 String passwordString = sharedPreferences.getString("password", ""); // 设置编辑框信息 password.setText(passwordString); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 通过edit 方法实例化Editor对象,储存信息 SharedPreferences.Editor editor = sharedPreferences.edit(); // 以键值对的形式储存信息 editor.putString("username", username.getText().toString()); editor.putString("password", password.getText().toString()); // 判断复选框的选中状态并进行存储 if (checkBox.isChecked()) { editor.putBoolean("checked", true); } else { editor.putBoolean("checked", false); } // 执行储存操作 editor.commit(); // 设置进度对话框 final ProgressDialog dialog = new ProgressDialog( MainActivity.this); // 设置标题 dialog.setTitle("用户登录"); // 设置提示信息 dialog.setMessage("正在登录,请稍后……"); // 开始进度对话框 dialog.onStart(); // 延时线程操作 new Thread() { @Override public void run() { // TODO Auto-generated method stub try { // 休眠3秒 Thread.sleep(3 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 对话框消失 dialog.dismiss(); // 设置意图转跳 Intent intent = new Intent(MainActivity.this, main.class); // 传递意图信息 intent.putExtra("username", username.getText() .toString()); // 开始activity转跳 startActivity(intent); MainActivity.this.finish(); } } }.start();// 开始线程操作 // 显示对话框 dialog.show(); } }); } }
登陆后的界面
package com.example.sharepreferencesdemo; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.widget.TextView; public class main extends Activity { private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.linearlayout); text=(TextView)this.findViewById(R.id.textView3); Intent intent=getIntent(); text.setTextSize(15); text.setGravity(Gravity.CENTER_HORIZONTAL); text.setTextColor(Color.CYAN); text.setText("欢迎: "+intent.getStringExtra("username")); } }
未点击记住密码,第二次登录。
点击记住密码登录
点击记住密码后,第三次登录
介绍了SharedPreferences属性文件的存储,我们可以进行较小数据的快速存储与便捷读取
下节预报:Internal
Storage内部存储器
相关文章推荐
- 从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
- 从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
- 从零开始学android<数据存储(2)Internal Storage内部存储.三十六.>
- 从零开始学android<数据存储(4)Sqlite数据库存储.三十八.>
- 从零开始学android<数据存储(3)External Storage外部存储.三十七.>
- 在AndroidManifest.xml文件中<activity>的android:windowSoftInputMode属性使用
- android数据存储<三>-----Sqlite操作
- Android系统开发 - 扩展Androidmanifest.xml文件中<application>标签的属性
- <Android>关于获取资源文件R中的数据
- 从零开始学android<android项目中各个文件及文件夹得作用.三.>
- 从零开始学android<ListView数据列表显示组件.二十一.>
- Android清单文件详解(五)----<application>的属性详解
- Android 读取清单文件<meta-data>元素的数据
- android 数据存储<一>----android短信发送器之文件的读写(手机+SD卡)
- Android数据存储五种方式总结<转载>
- Android中List与Map<String,Object>及SimpleAdapter用来存储数据并展示出来的联合用法
- Android清单文件详解(六) ---- <activity>节点的属性
- Android 读取<meta-data>元素的数据
- 【Android数据传递】Intent传递List和Object和List<Object>(附源码)
- android中对/data/data/<package name>/files下文件的读写操作