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

<?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"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#000000"
android:padding="10dp"
android:text="内部存储空间文件操作"
android:textColor="#ffffff"
android:textSize="25sp" /> <EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入你想输入的内容" /> <Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:onClick="click1"
android:text="写入"
android:textColor="#ffffff" /> <EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示读取的内容" /> <Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2C2A2A"
android:onClick="click2"
android:text="读取"
android:textColor="#ffffff" /> </LinearLayout>
package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
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);
}
public void click1(View view) {
String filename = "data.txt";
String content=((EditText)findViewById(R.id.et_1)).getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(filename, MODE_PRIVATE);
fos.write(content.getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null)
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "保存成功", 0).show();
} public void click2(View view) {
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
((EditText) findViewById(R.id.et_2)).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:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#27D52C"
android:padding="5dp"
android:text="用户登录"
android:textColor="#FFFFFF"
android:textSize="25sp" /> <LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:orientation="horizontal"> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="账号"
android:textColor="#000000"
android:textSize="18sp" /> <EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入账号" /> </LinearLayout> <LinearLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:orientation="horizontal"> <TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="密码"
android:textColor="#000000"
android:textSize="18sp" /> <EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入密码" /> </LinearLayout> <LinearLayout
android:id="@+id/ll_3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="25dp"
android:orientation="horizontal"> <CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="记住密码" /> <CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="自动登录" /> <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="登录" /> </LinearLayout> </LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private EditText et_account;
private EditText et_password;
private Button btn_login; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Map<String, String> userInfo = saveQQ.getUserInfo(this);
if (userInfo != null) {
et_account.setText(userInfo.get("account"));
et_password.setText(userInfo.get("password")); }
} private void initView() {
et_account = (EditText) findViewById(R.id.et1);
et_password = (EditText) findViewById(R.id.et2);
btn_login = (Button) findViewById(R.id.btn1);
btn_login.setOnClickListener(this);
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(this);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
cb2.setOnCheckedChangeListener(this);
} @Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString();
if(TextUtils.isEmpty(account)){
Toast.makeText(this,"输入QQ账号",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); } } @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.cb1 :
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString();
boolean isSaveSuccess=saveQQ.SaveUserInfo(this, account, password);
if(isSaveSuccess&&b){
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
et_account.setText(null);
et_password.setText(null);
} break;
case R.id.cb2: account=et_account.getText().toString().trim();
password=et_password.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;
}
if(b)
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
else{
Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
} }
}
}
package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences; import java.util.HashMap;
import java.util.Map; public class saveQQ {
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("account", 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("account",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;
} }

Android第六次作业的更多相关文章

  1. 17秋 软件工程 第六次作业 Beta冲刺 Scrum3

    17秋 软件工程 第六次作业 Beta冲刺 Scrum3 各个成员冲刺期间完成的任务 世强:完成手势签到模块,重构活动详情页面: 陈翔:完善超级管理员后端login模块,完成logout模块: 树民: ...

  2. 17秋 软件工程 第六次作业 Beta冲刺 总结博客

    题目:团队作业--Beta冲刺 17秋 软件工程 第六次作业 Beta冲刺 总结博客 Beta冲刺过程中各个成员的贡献百分比 世强:15.5% 陈翔:14.5% 树民:12.0% 媛媛:14.0% 港 ...

  3. 耿丹CS16-2班第六次作业汇总

    Deadline: 2016-11-13 11:59 作业内容 第六次作业总结 00.本次题目分值最高为**6分/题 × 7题 + 5分/篇 × 1篇 = 47分**,其中有新解法者每题加原创分**2 ...

  4. C2第六次作业解题报告

    看过题解后如果觉得还算有用,请帮忙加点我所在团队博客访问量 http://www.cnblogs.com/newbe/ http://www.cnblogs.com/newbe/p/4069834.h ...

  5. C语言程序设计第六次作业——循环结构(2)

    C语言程序设计第六次作业--循环结构(2) 之前的博客园图片没处理好,对大家说一声抱歉.希望大家能够多多指出我的错误,我来认真修改 ^ - ^ !. (1)改错题 序列求和:输入一个正实数eps,计算 ...

  6. 17秋 软件工程 第六次作业 Beta冲刺 Scrum1

    17秋 软件工程 第六次作业 Beta冲刺 Scrum1 各个成员冲刺期间完成的任务 重新梳理项目架构与当前进展,并且对我们的Alpha版本项目进行完整测试,将测试过程中发现的问题列入Github i ...

  7. 17秋 软件工程 第六次作业 Beta冲刺 Scrum2

    17秋 软件工程 第六次作业 Beta冲刺 Scrum2 我们组转会成员:杰麟: 我们组新成员:宏庆. 各个成员冲刺期间完成的任务 世强:完成分页功能的演示: 陈翔:完成超级管理员后端login模块: ...

  8. 17秋 软件工程 第六次作业 Beta冲刺 Scrum4

    17秋 软件工程 第六次作业 Beta冲刺 Scrum4 各个成员冲刺期间完成的任务 世强:完成APP用户签到模块.群发短信模块前端界面: 陈翔:恢复Github项目,完成Scrum博客: 树民:和超 ...

  9. 17秋 软件工程 第六次作业 Beta冲刺 Scrum5

    17秋 软件工程 第六次作业 Beta冲刺 Scrum5 各个成员冲刺期间完成的任务 世强:完成APP端相册.部员管理.手势签到模块: 陈翔:完成Scrum博客.总结博客,完成超级管理员前后端对接: ...

  10. 2018-2019-1 20189221 《Linux内核原理与分析》第六周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第六周作业 实验五 实验过程 将Fork函数移植到Linux的MenuOS fork()函数通过系统调用创建一个与原来 ...

随机推荐

  1. pycharm系列---django

    manage debug Python Console基本配置 DJANGO_SETTINGS_MODULE=mini_project.settings import sys import djang ...

  2. 通过Jenkins,执行远程服务器的自动化脚本

    通过Jenkins,可自动执行写好的应用部署.自动化测试等的脚本工具,实现测试环境的应用自动定时更新.自动执行测试等. 1. 安装SSH插件 进入[系统管理]-[插件管理]-[可用插件],搜索Publ ...

  3. elasticsearch聚合之bucket terms聚合

    目录 1. 背景 2. 前置条件 2.1 创建索引 2.2 准备数据 3. 各种聚合 3.1 统计人数最多的2个省 3.1.1 dsl 3.1.2 运行结果 3.2 统计人数最少的2个省 3.2.1 ...

  4. spring运行报500 bean不存在

    spring运行报500 bean不存在 bean不存在 步骤: ​ 查看bean是否注入成功 ​ junit单元测试 ​ 问题,不一定在我们底层,是spring出现了问题 ​ SpringMVC整合 ...

  5. Android Studio打开时报错if you already hava 64-bit JDK installed,define a JAVA_HOME

    出现这个问题不知道改了什么导致的,卸载了重新安装也是不行. 以及到高级设置中进行配置jdk也是无效, 解决方法为需要在路径 C:\Users\你自己的用户名\AppData\Roaming\Googl ...

  6. DHorse操作手册

    在介绍DHorse的操作之前,我们先来看一下发布一个系统的流程是什么样的. 发布系统的流程 我们以一个Springboot系统为例,来说明一下发布流程. 1.首先从代码仓库下载代码,比如Gitlab: ...

  7. Java外包程序员的技术出路

    学习的两个目的: 应付面试 应付工作(解决问题) 首先要明白学习的目的,不同阶段,不同技术的学习目的是不一样的. 有些技术,仅仅是应用级别的,有些技术是原理级别的(主要还是应试).所以不同技术.不同时 ...

  8. 批量将多个相同Excel表格内容合并到一个Excel表格的sheet工作簿当中。

    Sub Books2Sheets()Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) Dim ...

  9. 【每日一题】2021年12月6日-剑指 Offer 22. 链表中倒数第k个节点

    输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点. 例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1.2.3.4.5.6 ...

  10. 详解redis网络IO模型

    前言 "redis是单线程的" 这句话我们耳熟能详.但它有一定的前提,redis整个服务不可能只用到一个线程完成所有工作,它还有持久化.key过期删除.集群管理等其它模块,redi ...