热门标签 | HotTags
当前位置:  开发笔记 > 程序员 > 正文

第十一周十二周作业

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

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



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">
<EditText
android:id
="@+id/et1"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:hint
="输入你想输入的内容" />
<Button
android:id
="@+id/bt1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="写入"
android:onClick
="click1"/>
<EditText
android:id
="@+id/et2"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:hint
="显示读取的内容" />
<Button
android:id
="@+id/bt2"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="读取"
android:onClick
="click2"/>

package com.example.se;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View view) {
EditText et1
= findViewById(R.id.et1);
EditText et2
= findViewById(R.id.et2);
String filename
= "data.txt";
String content
= et1.getText().toString();
FileOutputStream fos
= null;
try {
fos
= openFileOutput(filename, MODE_PRIVATE);
fos.write(content.getBytes());
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (fos != null)
try {
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(
this, "保存成功", Toast.LENGTH_SHORT).show();
}
public void click2(View view) {
EditText et2
= findViewById(R.id.et2);
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 {
if (fis != null)
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
et2.setText(content);
}
}

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



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">
<TextView
android:id
="@+id/tv1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="账号"
android:layout_marginLeft
="50dp"
android:layout_marginTop
="10dp"
/>
<EditText
android:id
="@+id/et1"
android:layout_width
="300dp"
android:layout_height
="wrap_content"
android:hint
="请输入用户名"
android:layout_toRightOf
="@id/tv1"/>
<TextView
android:id
="@+id/tv2"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_marginLeft
="50dp"
android:text
="密码"
android:layout_marginTop
="10dp"
android:layout_below
="@id/et1"/>
<EditText
android:id
="@+id/et2"
android:layout_width
="300dp"
android:layout_height
="wrap_content"
android:hint
="请输入密码"
android:layout_below
="@id/et1"
android:layout_toRightOf
="@id/tv2"/>
<Button
android:id
="@+id/bt1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="登录"
android:textColor
="#00FFFF"
android:background
="#076453"
android:layout_below
="@id/cb1"
android:layout_marginLeft
="280dp"/>
<CheckBox
android:id
="@+id/cb1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="记住密码"
android:layout_below
="@id/et2"
android:layout_marginTop
="10dp"
android:layout_marginLeft
="50dp"/>
<CheckBox
android:id
="@+id/cb2"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="自动登录"
android:layout_below
="@id/et2"
android:layout_toRightOf
="@id/cb1"
android:layout_marginTop
="10dp"
android:layout_marginLeft
="20dp"/>

package com.example.we;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
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 androidx.annotation.CheckResult;
public class MainActivity extends Activity implements android.view.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
userInfo=re.getUserInfo(this);
if(userInfo!=null){
et_account.setText(userInfo.get(
"account"));
et_password.setText(userInfo.get(
"password"));
}
}
private void initView() {
// TODO Auto-generated method stub
et_account=(EditText)findViewById(R.id.et1);
et_password
=(EditText)findViewById(R.id.et2);
btn_login
=(Button)findViewById(R.id.bt1);
btn_login.setOnClickListener(
this);
CheckBox cb1
=findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(
this);
CheckBox cb2
=findViewById(R.id.cb2);
cb2.setOnCheckedChangeListener(
this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bt1:
String account
=et_account.getText().toString().trim();
String 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;
}
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=re.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.we;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class re{
public static boolean saveUserInfo(Context context,String account,String password ){
SharedPreferences sp
=context.getSharedPreferences("data", context.MODE_PRIVATE);
SharedPreferences.Editor editor
=sp.edit();
editor.putString(
"Username", account);
editor.putString(
"pwd", password);
editor.commit();
return true;
}
public static Map getUserInfo(Context context){
SharedPreferences sp
=context.getSharedPreferences("data", context.MODE_PRIVATE);
String account
=sp.getString("Username", null);
String password
=sp.getString("pwd", null);
Map
userMap=new HashMap();
userMap.put(
"account", account);
userMap.put(
"password", password);
return userMap;
}
}

 

 



推荐阅读
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • windows便签快捷键_用了windows十几年,没想到竟然这么好用!隐藏的功能你知道吗?
    本文介绍了使用windows操作系统时的一些隐藏功能,包括便签快捷键、截图功能等。同时探讨了windows和macOS操作系统之间的优劣比较,以及人们对于这两个系统的不同看法。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了使用CentOS7.0 U盘刻录工具进行安装的详细步骤,包括使用USBWriter工具刻录ISO文件到USB驱动器、格式化USB磁盘、设置启动顺序等。通过本文的指导,用户可以轻松地使用U盘安装CentOS7.0操作系统。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
author-avatar
SufiaLi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有