android数据存放map_android-数据存储(保存读取密码-附源码)

功能要求 : 用户名zhangsan密码123为正确登录状态

如果勾选记住密码会在登录之前保存 用户名密码到应用程序/files/infx.txt文件中

存储格式为用户名##密码

接下来 是 xml代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请输入用户名" />

android:id="@+id/et_username"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请输入密码 " />

android:id="@+id/et_password"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:inputType="textPassword" />

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

android:id="@+id/check"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="记住密码" />

android:id="@+id/login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:text="登录" />

接下来是mainactivity代码

package com.liwei.loginview;

import java.util.HashMap;

import java.util.Map;

import com.liwei.loginview.services.LoginService;

import android.os.Bundle;

import android.app.Activity;

import android.text.TextUtils;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.Checkable;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private EditText et_username;//用户名

private EditText et_password;//密码

private CheckBox ch;//check

private Button button;//登录按钮

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_username = (EditText) findViewById(R.id.et_username);

et_password = (EditText) findViewById(R.id.et_password);

ch = (CheckBox) findViewById(R.id.check);

button = (Button) findViewById(R.id.login);

//判断 是否有保存记录 如果不为空的话 取出显示到界面上

Map map = new HashMap();

map = LoginService.getSavedUserInfo(this);

if (map != null) {

et_username.setText(map.get("username"));

et_password.setText(map.get("password"));

}

button.setOnClickListener(this);

// 检查是否有保存的用户名密码 如果有 显示出来

}

@Override

public void onClick(View v) {

String name = et_username.getText().toString();

String pwd = et_password.getText().toString();

if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {

Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();

} else {

// 判断是否保存密码

if (ch.isChecked()) {

// 保存用户密码

boolean ruslt = LoginService.saveUserInfo(this, name, pwd);

if (ruslt) {

Log.i("TAG", "保存成功");

}

Log.i("TAG", "保存密码成功");

Toast.makeText(this, "保存密码成功", Toast.LENGTH_SHORT).show();

}

if ("zhangsan".equals(name) && "123".equals(pwd)) {

Log.i("TAG", "登陆成功");

Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();

} else {

Log.i("TAG", "登陆失败");

Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();

}

}

}

}

保存文件 读取文件业务方法

package com.liwei.loginview.services;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.nio.Buffer;

import java.util.HashMap;

import java.util.Map;

import android.content.Context;

public class LoginService {

// 没有使用任何类的成员变量 建议使用静态方法

public static boolean saveUserInfo(Context context, String username,

String password) {

try {

// File file = new File("/data/data/com.liwei.loginview/info.txt");

File file = new File(context.getFilesDir(), "info.txt"); //获取路径 如 "/data/data/com.liwei.loginview/files / 创建文件 info.txt

// context.getFilesDir();//返回一个目录/data/data/com.liwei.loginview+

// files

FileOutputStream fos = new FileOutputStream(file);

// zhangsan ## 123

fos.write((username + "##" + password).getBytes());

return true;

} catch (FileNotFoundException e) {

e.printStackTrace();

return false;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

public static Map getSavedUserInfo(Context context) {

File file = new File(context.getFilesDir(), "info.txt");

try {

FileInputStream fis = new FileInputStream(file);

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

String str = br.readLine();

String[] infos = str.split("##");

Map map = new HashMap();

map.put("username", infos[0]);

map.put("password", infos[1]);

return map;

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

} catch (IOException e) {

e.printStackTrace();

return null;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值