作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来

activity_practice2的layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp3.PractiseActivity2"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_username"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="bt_dl_onClick"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="注册"
android:onClick="bt_zc_onClick"
android:layout_weight="1"/>
</LinearLayout> <!--<Button-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="fdasf"-->
<!--android:onClick="bt_onClick"-->
<!--android:id="@+id/bt_1"/>-->
</LinearLayout>

activity_practice2_2的layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp3.PractiseActivity2_2"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名称"
android:id="@+id/et_username_1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="登陆密码"
android:id="@+id/et_password_1"
android:layout_below="@id/et_username_1"
android:inputType="textPassword"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="个性签名"
android:id="@+id/et_gxqm"
android:layout_below="@id/et_password_1"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:layout_weight="1"
android:onClick="bt_qd_onClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:layout_weight="1"
android:onClick="bt_qx_onClick"/>
</LinearLayout>
<!--<Button-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="fdasf"-->
<!--android:onClick="bt_onClick"-->
<!--android:id="@+id/bt_1"-->
<!--android:layout_below="@+id/et_gxqm"/>-->
</RelativeLayout>

activity_practice2_3的layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp3.PractiseActivity2_3"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_username"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_gxqm"
android:layout_below="@id/tv_username"
android:textSize="30dp"/>
</RelativeLayout>

PractiseActivity2的java类:

 package com.hanqi.testapp3;

 import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileInputStream; public class PractiseActivity2 extends AppCompatActivity { EditText et_username;
EditText et_password; // Button bt_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2);
et_username = (EditText)findViewById(R.id.et_username);
et_password = (EditText)findViewById(R.id.et_password);
//bt_1 = (Button)findViewById(R.id.bt_1);
}
public void bt_zc_onClick(View v)
{
Intent intent = new Intent(this,PractiseActivity2_2.class);
startActivity(intent);
} public void bt_dl_onClick(View v)
{
String str1 = "";
String str2 = "";
String str3 = "";
String uc = et_username.getText().toString();
String pw = et_password.getText().toString(); try {
FileInputStream fis = openFileInput("practise2-1.txt");
byte[] b = new byte[1024];
int i; while ((i=fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
}
fis.close(); FileInputStream fis2 = openFileInput("practise2-2.txt");
byte[] b2 = new byte[1024];
int i2 = 0;
while ((i2=fis2.read(b2))>0)
{
String str = new String(b2,0,i2);
str2 += str;
}
fis2.close(); FileInputStream fis3 = openFileInput("practise2-3.txt");
byte[] b3 = new byte[1024];
int i3 = 0;
while ((i3=fis3.read(b3))>0)
{
String str = new String(b3,0,i3);
str3 += str;
}
fis3.close();
}
catch (Exception e)
{ }
if (uc.trim().length()==0||pw.trim().length()==0)
{
Toast.makeText(PractiseActivity2.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (str1 ==null||(str1!=null&&!str1.equals(uc)))
{
Toast.makeText(PractiseActivity2.this, "用户未注册", Toast.LENGTH_SHORT).show();
return;
}
if (!str2.equals(pw))
{
Toast.makeText(PractiseActivity2.this, "密码错误", Toast.LENGTH_SHORT).show();
return;
}
else
{
Toast.makeText(PractiseActivity2.this, "用户验证成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,PractiseActivity2_3.class);
startActivity(intent);
finish();
}
}
// public void bt_onClick(View v)
// {
// try {
// FileInputStream fis = openFileInput("practise2-1.txt");
// byte[] b = new byte[1024];
// int i;
//
// while ((i=fis.read(b))>0)
// {
// String str = new String(b,0,i);
// str1 += str;
// }
//
// fis.close();
// }
// catch (Exception e)
// {
//
// }
// bt_1.setText(str1);
// }
}

PractiseActivity2_2的java类:

 package com.hanqi.testapp3;

 import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileOutputStream;
import java.io.PrintStream; public class PractiseActivity2_2 extends AppCompatActivity { EditText et_username_1;
EditText et_password_1;
EditText et_gxqm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2_2);
et_username_1 = (EditText)findViewById(R.id.et_username_1);
et_password_1 = (EditText)findViewById(R.id.et_password_1);
et_gxqm = (EditText)findViewById(R.id.et_gxqm);
}
public void bt_qd_onClick(View v)
{
String username = et_username_1.getText().toString();
if (username ==null||username.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "请填写用户名", Toast.LENGTH_SHORT).show();
return;
}
String password = et_password_1.getText().toString();
if (password ==null||password.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "请填写密码", Toast.LENGTH_SHORT).show();
return;
}
String gxqm = et_gxqm.getText().toString();
if (gxqm ==null||gxqm.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "个性签名为必填内容", Toast.LENGTH_SHORT).show();
return;
}
try {
FileOutputStream fos = openFileOutput("practise2-1.txt",MODE_PRIVATE);
PrintStream ps = new PrintStream(fos);
ps.print(username);
FileOutputStream fos2 = openFileOutput("practise2-2.txt",MODE_PRIVATE);
PrintStream ps2 = new PrintStream(fos2);
ps2.print(password);
FileOutputStream fos3 = openFileOutput("practise2-3.txt",MODE_PRIVATE);
PrintStream ps3 = new PrintStream(fos3);
ps3.print(gxqm);
ps.close();
ps2.close();
ps3.close();
fos.close();
Toast.makeText(PractiseActivity2_2.this, "保存成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{ }
Intent intent = new Intent(this,PractiseActivity2.class);
startActivity(intent);
finish();
}
public void bt_qx_onClick(View v)
{
setResult(RESULT_CANCELED, null);
finish();
} }

PractiseActivity2_3的java类:

 package com.hanqi.testapp3;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; import java.io.FileInputStream; public class PractiseActivity2_3 extends AppCompatActivity { TextView tv_username;
TextView tv_gxqm; String str1 = "";
String str3 = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2_3);
tv_username = (TextView)findViewById(R.id.tv_username);
tv_gxqm = (TextView)findViewById(R.id.tv_gxqm); try {
FileInputStream fis = openFileInput("practise2-1.txt");
byte[] b = new byte[1024];
int i; while ((i=fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
} fis.close(); FileInputStream fis3 = openFileInput("practise2-3.txt");
byte[] b3 = new byte[1024];
int i3 = 0;
while ((i3=fis3.read(b3))>0)
{
String str = new String(b3,0,i3);
str3 += str;
}
fis3.close();
}
catch (Exception e)
{ }
tv_username.setText("用户名是:" + str1);
tv_gxqm.setText("个性签名是:"+str3);
}
}

效果为:

Android——手机内部文件存储(作业)的更多相关文章

  1. Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  2. Android入门(九)文件存储与SharedPreferences存储

    原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...

  3. android-数据存储之手机内部file存储

    一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4&g ...

  4. (转)获取android手机内部存储空间和外部存储空间的参数 && 如何决定一个apk的安装位置

    转:http://blog.csdn.net/zhandoushi1982/article/details/8560233 获取android文件系统的信息,需要Environment类和StatFs ...

  5. Android手机QQ文件夹解析

    注:切勿修改手机QQ文件夹,以免造成不必要的使用问题及无法修复的数据丢失] 安卓手机QQ tencent文件夹解析 QQ下载的聊天背景:tencent→MobileQQ→system_backgrou ...

  6. 【Android】14.1 内部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...

  7. android之外部文件存储和读取

    这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可.这里需要对获取SDcard的读写权限. 一.AndroidManifest.xml 这里增加了对外部存储设备的读写权限 <?xml ...

  8. android面试(4)---文件存储

    1.sharePreference? SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. SharedPreferences保存数据,其背后是用xml文件存放 ...

  9. android app 内部文件路径

    public class MainActivity extends Activity { final String FILE_NAME = "crazyit.bin"; @Over ...

随机推荐

  1. myeclipse/eclipse没有Project Facets的解决方法

    http://www.cnblogs.com/jerome-rong/archive/2012/12/18/2822783.html 经常在eclipse中导入web项目时,出现转不了项目类型的问题, ...

  2. 最精简的django程序

    一.程序框架 1.结构图

  3. python的类和对象——类成员番外篇

    学完了面向对象的三大特性,已经get了所有屌丝技能的我们也当一回文艺小青年,来看看类的成员和成员修饰符. 今天‘三’这个数字好亲和~~~类成员可以分为三类:字段.方法和属性 一.字段 首先我们来看看字 ...

  4. 初学JAVA

    通过eclipse开发java程序:1.创建一个java项目 2.创建程序包 3.编写java源程序 4.运行java程序 例如开发"helloworld "java程序: 1.进 ...

  5. JavaWeb高级:Servlet源码分析

    很多东西归根结底是对Servlet源代码的了解,通过学习源代码加深了解Java高级特性

  6. Windows Store App 全球化:在XAML元素中引用字符串资源

    在应用程序中可以通过XAML元素和后台代码两种方式引用资源文件中的字符串资源.本小节先讲述如何在XAML元素中引用字符串资源的相关知识点. 在XAML元素中可以通过使用x:Uid属性来引用资源文件中的 ...

  7. 自己封装的一个java图片验证码

    验证码生成器: package com.lz.Tools; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; ...

  8. ASP.NET中的Session怎么正确使用[转]

    Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息.用户在应用程序的页面切换时,Session对象的变量不会被清除. 对于一个Web应用 ...

  9. WCF初探-5:WCF消息交换模式之双工通讯(Duplex)

    双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服务端处理完请求后,返回给客户端的不是reply ...

  10. iOS开发UI篇—从代码的逐步优化看MVC

    iOS开发UI篇—从代码的逐步优化看MVC 一.要求 要求完成下面一个小的应用程序. 二.一步步对代码进行优化 注意:在开发过程中,优化的过程是一步一步进行的.(如果一个人要吃五个包子才能吃饱,那么他 ...