Android第十一、十二周作业
图片一 用内部存储实现文件写入和读取功能
<?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;
}
} }

Android第十一、十二周作业的更多相关文章
- 2017-2018-1 《Linux内核原理与设计》第十二周作业
<linux内核原理与设计>第十二周作业 Sql注入基础原理介绍 分组: 和20179215袁琳完成实验 一.实验说明 SQL注入攻击通过构建特殊的输入作为参数传入Web应用程序,而这 ...
- 2019-2020-1 20199329《Linux内核原理与分析》第十二周作业
<Linux内核原理与分析>第十二周作业 一.本周内容概述: 通过编程理解 Set-UID 的运行机制与安全问题 完成实验楼上的<SET-UID程序漏洞实验> 二.本周学习内容 ...
- 第十二周作业_PSP总结报告
回顾1 (1)回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你认为过去接触到的课程是否符合你对计算机专业的期待,为什么?经过一个学 ...
- 2019春第十二周作业Compile Summarize
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 这里 我在这个课程的目标是 能按自己的想法解出题目 这个作业在那个具体方面帮助我实现目标 能朝着软件工程师方向发展 参考文献与网址 C语言 ...
- C语言第十二周作业
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/h ...
- 20169212《Linux内核原理及分析》第十二周作业
格式化字符串漏洞实验 格式化字符串漏洞是由像 printf(user_input) 这样的代码引起的,其中 user_input 是用户输入的数据,具有 Set-UID root 权限的这类程序在运行 ...
- 201521123027 <java程序设计>第十二周作业总结
1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2.书面作业 将Student对象(属性:int id, String name,int age,double ...
- python第六十八天--第十二周作业
主题: 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条 ...
- 2017-2018-1 20179202《Linux内核原理与分析》第十二周作业
C语言实现Linux网络嗅探器 一.知识准备 1.一般情况下,网络上所有的机器都可以"听"到通过的流量,但对不属于自己的数据包则不予响应.如果某个工作站的网络接口处于混杂模式,那么 ...
随机推荐
- CF1479B Painting the Array(贪心+DP)
题目大意:给你一个序列,让你提取出一个子序列A,剩余的部分组成子序列B,现定义seg(x)表示把序列x中相邻的相同数合并成一个数后,序列x的长度,分别求seg(A)+seg(B)的最大值和最小值,n= ...
- MVC与MVVM?
model-数据层 view-视图层 controller-控制层 MVC的目的是实现M和V的分离,单向通信,必须通过C来承上启下 MVVM中通过VM(vue中的实例化对象)的发布者-订阅者模式实现双 ...
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
abstract的method 不可以是static的,因为抽象的方法是要被子类实现的,而static与子类扯不上关系! native方法表示该方法要用另外一种依赖平台的编程语言实现的,不存在着被子类 ...
- jQuery--筛选【串联函数】
串联函数简介 A.add(B) 将A和B组合成一个对象 A.children().andSelf() 将之前的对象添加到操作集合中 A.children().children().end() 回到最近 ...
- ACL 权限控制机制 ?
UGO(User/Group/Others) 目前在 Linux/Unix 文件系统中使用,也是使用最广泛的权限控制方式.是一种粗 粒度的文件系统权限控制模式. ACL(Access Control ...
- 说出 5 条 IO 的最佳实践?
IO 对 Java 应用的性能非常重要.理想情况下,你不应该在你应用的关键路径上 避免 IO 操作.下面是一些你应该遵循的 Java IO 最佳实践: a)使用有缓冲区的 IO 类,而不要单独读取字节 ...
- List、Set、Map 和 Queue 之间的区别?
List 是一个有序集合,允许元素重复.它的某些实现可以提供基于下标值的常量 访问时间,但是这不是 List 接口保证的.Set 是一个无序集合.
- WSGI是个啥?大白话告诉你wsgi做了什么!
定义: 官方定义:wsgi是Web服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI)是为Python语言定义的Web服务器和Web应用程序或框架之 ...
- 为什么HTTP/3要基于UDP?可靠吗?
目录 前言 为什么转用UDP? HTTP/3解决了那些问题? 队头阻塞问题 QPACK编码 Header 参考 推荐阅读: 计算机网络汇总 HTTP/3竟然是基于UDP的!开始我也很疑惑,UDP传输不 ...
- 『忘了再学』Shell基础 — 7、Bash基本功能(多命令顺序执行)
目录 1.多命令执行符: 2.多命令执行符&& 3.多命令执行符|| 4.&&和||联合应用 Linux系统支持多条命令顺序执行,就是我可以依次输入多条命令后,统一按E ...