图片一 用内部存储实现文件写入和读取功能

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="内部存储空间文件操作"
android:textSize="20sp"
android:textColor="#FFB6C1"/> <EditText
android:id="@+id/a1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入你想要写入的内容" /> <Button
android:id="@+id/b1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="写入"
android:textColor="#000000"
android:onClick="click1"/> <EditText
android:id="@+id/a2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示读取的内容" /> <Button
android:id="@+id/b2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="读取"
android:textColor="#000000"
android:onClick="click2"/> </LinearLayout>
package com.example.gcgc;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.view.View;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@SuppressLint("WrongConstant")
public void click1(View view){
EditText a1=findViewById(R.id.a1);
EditText a2=findViewById(R.id.a2);
String fileName ="data.txt";
String content =a1.getText().toString();
FileOutputStream fos =null;
try{
fos = openFileOutput(fileName,MODE_PRIVATE);
fos.write(content.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally {
try{
if (fos!=null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
Toast.makeText(this,"保存成功",0).show();
}
@SuppressLint("WrongConstant")
public void click2(View view){
EditText a2 =findViewById(R.id.a2);
String content="";
FileInputStream fis =null;
try{
fis = openFileInput("data.txt");
byte[] buffer =new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
a2.setText(content);
}
}
}

图片二 使用sharedpreference实现记住密码功能

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户登录"
android:textColor="#FFC0CB"
android:textSize="30dp"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textColor="#D8BFD8"
android:textSize="20dp"/> <EditText
android:id="@+id/a1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textColor="#D8BFD8"
android:textSize="20dp"/> <EditText
android:id="@+id/a2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <CheckBox
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="15dp" /> <CheckBox
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:textSize="15dp" /> <Button
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="20dp"
android:textColor="#9370DB"/>
</LinearLayout>
</LinearLayout>
package com.example.gc1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SharedMemory;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.Toast; import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText a1;//账号输入框
private EditText a2;//密码输入框
private Button c1;//登录按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
Map<String,String>userInfo = SPSaveQQ.getUserInfo(this);
if (userInfo!=null){
a1.setText(userInfo.get("account"));
a2.setText(userInfo.get("pssword"));
}
} private void initView() {
a1=(EditText)findViewById(R.id.a1);
a2=(EditText)findViewById(R.id.a2);
c1=(Button)findViewById(R.id.c1);
c1.setOnClickListener(this);
} public void onClick(View view){
switch (view.getId()){
case R.id.c1:
String account =a1.getText().toString().trim();
String password =a2.getText().toString();
if (TextUtils.isEmpty(account)){
Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
boolean isSaveSuccess =SPSaveQQ.saveUserInfo(this,account,password);
if (isSaveSuccess){
Toast.makeText(this,"保存成功", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show(); }
break;
}
} public static class SPSaveQQ{
public static boolean saveUserInfo(Context context,String account,String password){
SharedPreferences sp =context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor edit =sp.edit();
edit.putString("userName",account);
edit.putString("pwd",password);
edit.commit();
return true;
}
public static Map<String,String> getUserInfo(Context context){
SharedPreferences sp =context.getSharedPreferences("data",Context.MODE_PRIVATE);
String account =sp.getString("userName",null);
String password =sp.getString("pwd",null);
Map<String,String> userMap =new HashMap<String, String>();
userMap.put("account",account);
userMap.put("password",password);
return userMap;
}
} }

最新文章

  1. 【.net部署】Server Error in &#39;/&#39; Application.错误解决方案
  2. L#脚本语言,直接把DLL当脚本执行(图解说明)
  3. Bootstrap+angularjs+MVC3+分页技术+角色权限验证系统
  4. typeof instanceof
  5. poi 读取 excel (.xls) 97-2003
  6. 被 Windows 10 SDK 坑了
  7. C语言 电梯函数
  8. Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装
  9. 局域网内使用linux的ntp服务
  10. JavaScripts学习日记——ECMAscript
  11. [TYVJ] P1004 滑雪
  12. NodeJs之进程守护
  13. ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
  14. HTML——CSS的基础语法1
  15. Extjs6随笔(终篇)——内容总结
  16. GDB 资料汇总
  17. 使用ajax+php+mysql实现数据库定时刷新
  18. fork()相关的源码解析
  19. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)
  20. springboot笔记1(转载于puresmile)

热门文章

  1. 内网渗透----使用mimikatz获取windows登陆口令
  2. C++11移动语义之一(基本概念)
  3. Kerberos与各大组件的集成
  4. 如何在 Spring Boot 中禁用 Actuator 端点安全性?
  5. java-规约-OOP
  6. 事务的 ACID 是指什么?
  7. Spring Bean生命周期回调
  8. 学习Solr(一)
  9. Effective Java —— 用静态工厂方法代替构造器
  10. 设置python 虚拟环境 virtualenv django 虚拟环境