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.一般情况下,网络上所有的机器都可以"听"到通过的流量,但对不属于自己的数据包则不予响应.如果某个工作站的网络接口处于混杂模式,那么 ...
随机推荐
- java的https的get请求
package com.wl.webservice; import java.io.InputStream; import java.net.HttpURLConnection; import jav ...
- weblogic重要漏洞记录
(PS:之前在freebuf发过,这里直接复制过来的,所以有些图片会有水印) 前言 T3协议存在多个反序列化漏洞CVE-2015-4852/CVE-2016-0638/CVE-2016-3510/CV ...
- java三种适配器模式详解与代码实现
zhaoyu 取消关注 2 人赞同了该文章 1. 适配器模式定义: 适配器模式是一种结构型设计模式,通过一个适配器类把具有不同方法功能的两个类A和B组合起来,使得这个适配器类同时具有两个类的不 ...
- 【转】pringMVC+Hibernate+Spring 简单的一个整合实例
ref:http://langgufu.iteye.com/blog/2088355 SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世早而占据了一定优势,我在博客 ...
- 客户端回调 Watcher ?
客户端 SendThread 线程接收事件通知,交由 EventThread 线程回调 Watcher. 客户端的 Watcher 机制同样是一次性的,一旦被触发后,该 Watcher 就失效了.
- 为什么需要域驱动设计DDD?
我们需要 DDD 的因素 – 微服务面试问题
- Netty学习摘记 —— UDP广播事件
本文参考 本篇文章是对<Netty In Action>一书第十三章"使用UDP广播事件"的学习摘记,主要内容为广播应用程序的开发 消息POJO 我们将日志信息封装成名 ...
- 阿里云、腾讯云、CentOS下的MySQL的安装与配置详解
一. 安装 查看是否已安装 # 查看MySQL版本 mysql --version # 查看MySQL相关文件 whereis mysql 若已安装,卸载方法如下 # 卸载MySQL yum remo ...
- 手把手教你学vue-4(vuex)
1.首先明白vuex是做什么用的. 管理统一组件状态state.每个应用将仅仅包含一个 store 实例.单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态 ...
- C#编写一个控制台应用程序,可根据输入的月份判断所在季节
编写一个控制台应用程序,可根据输入的月份判断所在季节 代码: using System; using System.Collections.Generic; using System.Linq; us ...