用户登录
目的:
完成EditText中输入用户名和密码,并存储在私人目录中以备调用
1、 存储输入的内容:
利用Android Context类openFileOutput方法
FileOutputStream fileOutputStream = context.openFileOutput("userinfo", context.MODE_PRIVATE);
关于openFileOutput文档注释:
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
No permissions are required to invoke this method, since it uses internal storage.
Return:
The resulting FileOutputStream.
关于FileOutputStream writes bytes to a file 所以
fileOutputStream.write(userinfo.getBytes());
An output stream that writes bytes to a file. If the output file exists, it can be replaced or appended to. If it does not exist, a new file will be created.
2、 回显存储的信息
FileInputStream fileInputStream = context.openFileInput("userinfo");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String readLine = bufferedReader.readLine();
String[] split = readLine.split("##");
HashMap hashMap = new HashMap();
hashMap.put("username", split[0]);
hashMap.put("password", split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap;
参看Java内容
Mainactivity部分
Mapmap=UserInfoUtil.getUserInfo(mContext);
if(map !=null){
String username = map.get("username");
String password = map.get("password");
et_username.setText(username);
et_password.setText(password);
cb_remPass.setChecked(true);
}
这篇博客介绍了如何在Android应用中实现用户登录时EditText输入的用户名和密码的存储与回显。通过使用AndroidContext的openFileOutput方法将信息写入私人目录,然后使用FileInputStream和BufferedReader读取并解析存储的内容,最后将信息填充到EditText中。整个过程确保了数据的安全性和用户体验。

被折叠的 条评论
为什么被折叠?



