android学习笔记(一)数据存储与访问
来源:互联网 发布:淘宝长尾词是什么意思 编辑:程序博客网 时间:2024/05/22 21:48
Android为数据存储提供了以下几种方式:
1.文件存储方式
2.SharedPreference存储方式
3.Content Provider 内容提供者
4.网络存储方式
(一).文件操作方式(其本质即为输入输出流的操作)
实现登录界面账号、密码的保存功能。
1.写布局文件,分别添加:
a.图片控件(ImageView)
b.两个输入框(EditText)
c. 单选框(CheckBox)
d. 登录按钮(Button)
<span style="font-size:14px;"><LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.weimitechnology.QQLogin.MainActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="20dip"> <ImageView android:layout_width="92dp" android:layout_height="match_parent" android:src="@drawable/f25" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/QQ_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入QQ号码"/> <!-- 密码不能以明文方式显示 --> <EditText android:id="@+id/QQ_password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="请输入QQ密码"/> </LinearLayout> </LinearLayout> <CheckBox android:id="@+id/QQChecked" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:layout_gravity="center" android:text="记住密码"/> <Button android:id="@+id/QQLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="登录"/> </LinearLayout></span>
2.构建一个工具类(其中两个函数,分别实现登信息的保存、恢复)
a. 函数一: public static boolean saveUserInfo(String number, String password)
参数 number:传入输入框中的账号信息
b.函数二: public static Map<String, String> getUserInfor()参数 password: 传入输入框中的密码信息
备注;当用户选择保存密码,且登录成功后,保存用户登录信息,下次打开应用时自动返回登录账号密码。
返回类型:Map返回用户账号密码信息
备注:OnCreate(Bundle savedInstanceState)中调用
package com.weimitechnology.QQLogin.utils;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.text.TextUtils;import android.util.Log;public class Utills {/* 函数一:保存账号 密码 */public static boolean saveUserInfo(String number, String password){// 将数据写入文件的步骤:try { // 1.指定文件路径String path = "/data/data/com.weimitechnology.QQLogin/QQLogin.txt";FileOutputStream fosFileOutputStream = new FileOutputStream(path);// 2.给出所要写入的内容 54321##123456String data = number + "##" + password;// 3.以字节流的方式写入文件fosFileOutputStream.write(data.getBytes());// 4.刷新数据fosFileOutputStream.flush();// 5.关闭数据流fosFileOutputStream.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}/*函数二:账号密码信息的恢复*/public static Map<String, String> getUserInfor(){// 读取数据的步骤:try {// 1.获取文件存储路径String path = "/data/data/com.weimitechnology.QQLogin/QQLogin.txt";FileInputStream fis = new FileInputStream(path);// 2.构建了一个字符流BufferedReader reader= new BufferedReader(new InputStreamReader(fis));// 3.获取文本信息String text = reader.readLine();// 4.对文本信息进行分割解析if(!TextUtils.isEmpty(text)){String[] spit = text.split("##");Map<String, String> userInforMap = new HashMap<String, String>();Log.d("getUserInfor()",spit[0]+";"+spit[1]);userInforMap.put("number", spit[0]);userInforMap.put("password",spit[1]);fis.close();return userInforMap;}} catch (Exception e) {e.printStackTrace();}return null;}}
文件操作归纳:
(1).写入文本信息
1.定义写入路径并获得输出流
String path = "/data/data/包名/文件名"
FileOutputStream fos = new FileOutputStream(path);
2.获取所需写入的数据
String data = number + "##" + password;
3.写入数据
fos.write(data.getBytes());
4.刷新数据,关闭输入输出流
fos.flush();
fos.close();
(2).读取文本信息
1.指定路径并获得输入流
String path = "/data/data/包名/文件名"
FileInputStream fis = new FileItputStream(path);
2.获取字符流对象
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
3.读取文本信息
String text = reader.readLine();
4.关闭输入输出流
fis.close();
(3).对读取数据进行解析
从文本中所读取的数据 text = "98765##43210",中间的 ## 为写入文本信息是添加的分隔符:
if(!TextUtils.isEmpty(text))// 如果所读取道到得文本信息不为空
{
String[] spit = text.split("##");// 对文本中 ## 信息进行分割
Map<String, String> userInforMap = new HashMap<String, String>(); // 新建Map<String, String> 对象
Log.d("getUserInfor()",spit[0]+";"+spit[1]);
userInforMap.put("number", spit[0]);// key-values对应,spit[0]中存放的是账号信息
userInforMap.put("password",spit[1]);// key-values对应,spit[1]中存放的是密码信息
fis.close();
return userInforMap;
}
3.Activity中实现登录及信息保存、回显
package com.weimitechnology.QQLogin;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.weimitechnology.QQLogin.utils.Utills;public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity";private EditText qNumber;private EditText qPassword;private CheckBox qCheck;private Button qLogin;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* // 去除标题栏,必须在setContentView之前调用 requestWindowFeature(Window.FEATURE_NO_TITLE);*/ setContentView(R.layout.activity_main); qNumber = (EditText) findViewById(R.id.QQ_number); qPassword = (EditText) findViewById(R.id.QQ_password); qCheck = (CheckBox) findViewById(R.id.QQChecked); qLogin = (Button) findViewById(R.id.QQLogin); qLogin.setOnClickListener(this); // 回显数据 Map<String, String> userInforMap = Utills.getUserInfor(); if(userInforMap != null){ Log.d(TAG," 显示账号、密码:" + userInforMap.get("number") +"," + userInforMap.get("password")); qNumber.setText(userInforMap.get("number")); qPassword.setText(userInforMap.get("password")); } }@Overridepublic void onClick(View v) {// 执行登录操作// 1.取出除账号和密码String QQnumber = qNumber.getText().toString();String QQpassword = qPassword.getText().toString();if(TextUtils.isEmpty(QQnumber) || TextUtils.isEmpty(QQpassword)){Toast.makeText(this, "账号、密码不能为空", Toast.LENGTH_SHORT).show();return;}// 2.判断记住密码是否被选中,如果被选中存储起来if(qCheck.isChecked()){Log.d(TAG,"记住密码:" + QQnumber +"," + QQpassword);boolean isSuccess = Utills.saveUserInfo(QQnumber, QQpassword);if(isSuccess){Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);}else{Toast.makeText(this, "保存失败",Toast.LENGTH_SHORT);}}// 3.登录成功Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();}}
- android学习笔记(一)数据存储与访问
- Android学习笔记---android数据存储与访问
- android学习笔记6--------------数据的存储与访问(1)
- android学习笔记8--------------数据的存储与访问(2)
- Android学习笔记(基础)之数据存储(一)
- Android学习 —— 数据的存储与访问方式一: 文件存取
- 学习笔记 第7章 数据存储与访问
- Android学习笔记--资料存储与共享(一)
- android数据存储与访问
- Android数据存储与访问
- Android数据存储与访问
- Android数据存储与访问
- 《Python数据分析与展示》学习笔记(一)numpy数据存储与函数
- android数据存储与访问---文件存储
- android开发之数据存储与访问(一)—File
- 数据存储与访问一之文件
- 实验 Android数据存储和访问-SQLite(一)
- Android学习 —— 数据的存储与访问方式二: SharedPreferences(偏好参数保存)
- Scala ListBuffer使用备忘
- Apache Storm提交Topology时的默认CLASSPATH问题
- ClassNotFoundException when unmarshalling 问题总结
- Hibernate自动生成实体类注解
- Oracle 11.2.0.1在xp上的静默(slient)安装
- android学习笔记(一)数据存储与访问
- 微信企业号终于来了 第一时间带你体验真面目
- 在使用get传递中文参数的编码问题
- scala学习笔记:理解类继承
- HDU -- 1051 Wooden Sticks(贪心)
- cocos2d-x-3.4在WIN7下安装及新建一个工程
- swig android的使用
- leetcode 日经贴,python code -Excel Sheet Column Title
- Android数据存储与IO之三(File存储之ExternalStorage)