zoukankan      html  css  js  c++  java
  • 手机内存文件存储练习

    把用户的注册信息存储到文件里,登录成功后读出并显示出来,把代码发到博客上。

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.wang.xuexi.NCMActivity">
    11 
    12     <EditText
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:hint="用户名称"
    16         android:id="@+id/et_2"/>
    17     <EditText
    18         android:layout_width="match_parent"
    19         android:layout_height="wrap_content"
    20         android:hint="密码"
    21         android:inputType="numberPassword"
    22         android:layout_below="@id/et_2"
    23         android:id="@+id/et_3"/>
    24     <LinearLayout
    25         android:layout_width="match_parent"
    26         android:layout_height="wrap_content"
    27         android:layout_below="@id/et_3">
    28         <Button
    29             android:layout_width="0dp"
    30             android:layout_height="wrap_content"
    31             android:text="登陆"
    32             android:id="@+id/bt_1"
    33             android:onClick="bt1_OnClick"
    34             android:layout_weight="1"/>
    35         <Button
    36             android:layout_width="0dp"
    37             android:layout_height="wrap_content"
    38             android:text="注册"
    39             android:id="@+id/bt_2"
    40             android:onClick="bt2_OnClick"
    41             android:layout_weight="1"/>
    42     </LinearLayout>
    43 </RelativeLayout>
    登录界面.xml
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.wang.xuexi.NCZActivity"
    11     android:orientation="vertical">
    12 
    13     <EditText
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:hint="用户名称"
    17         android:id="@+id/et_1"/>
    18     <EditText
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:hint="用户代码"
    22         android:id="@+id/et_2"/>
    23     <EditText
    24         android:layout_width="match_parent"
    25         android:layout_height="wrap_content"
    26         android:hint="密码"
    27         android:id="@+id/et_3"/>
    28     <LinearLayout
    29         android:layout_width="match_parent"
    30         android:layout_height="wrap_content"
    31         >
    32         <Button
    33             android:layout_width="0dp"
    34             android:layout_height="wrap_content"
    35             android:text="取消"
    36             android:id="@+id/bt_1"
    37             android:layout_weight="1"/>
    38         <Button
    39             android:layout_width="0dp"
    40             android:layout_height="wrap_content"
    41             android:text="确定"
    42             android:id="@+id/bt_2"
    43             android:layout_weight="1"
    44             android:onClick="bt1_OnClick"/>
    45     </LinearLayout>
    46 
    47 </LinearLayout>
    注册.xml
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.wang.xuexi.NCTActivity"
    11     android:orientation="vertical">
    12 
    13 
    14     <TextView
    15         android:layout_width="match_parent"
    16         android:layout_height="60dp"
    17         android:id="@+id/tv_1"/>
    18 
    19     <TextView
    20         android:layout_width="match_parent"
    21         android:layout_height="60dp"
    22         android:id="@+id/tv_2"/>
    23 
    24     <TextView
    25         android:layout_width="match_parent"
    26         android:layout_height="60dp"
    27         android:id="@+id/tv_3"/>
    28 
    29 </LinearLayout>
    显示界面.xml
      1 package com.example.wang.xuexi;
      2 
      3 import android.content.Intent;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.os.Bundle;
      6 import android.view.View;
      7 import android.widget.EditText;
      8 import android.widget.Toast;
      9 
     10 import java.io.FileInputStream;
     11 
     12 public class NCMActivity extends AppCompatActivity {
     13 
     14     EditText et_2;
     15 
     16     EditText et_3;
     17 
     18 
     19     @Override
     20     protected void onCreate(Bundle savedInstanceState) {
     21         super.onCreate(savedInstanceState);
     22         setContentView(R.layout.activity_ncm);
     23 
     24         et_2=(EditText)findViewById(R.id.et_2);
     25         et_3=(EditText)findViewById(R.id.et_3);
     26     }
     27 
     28     public void bt2_OnClick(View v)
     29     {
     30         Intent intent=new Intent(this,NCZActivity.class);
     31 
     32         startActivity(intent);
     33     }
     34 
     35 
     36 
     37     public void bt1_OnClick(View v)
     38     {
     39         String name="";
     40         String code="";
     41         String pword="";
     42 
     43         try {
     44 
     45             FileInputStream fis1=openFileInput("name.txt");
     46             byte [] b1=new byte[1024];
     47             int i1=0;
     48             while ((i1=fis1.read(b1))>0)
     49             {
     50                 String name1=new String(b1,0,i1);
     51 
     52                 name+=name1;
     53             }
     54             fis1.close();
     55 
     56             FileInputStream fis2=openFileInput("code.txt");
     57             byte [] b2=new byte[1024];
     58             int i2=0;
     59             while ((i2=fis2.read(b2))>0)
     60             {
     61                 String code1=new String(b2,0,i2);
     62 
     63                 code+=code1;
     64             }
     65             fis2.close();
     66 
     67             FileInputStream fis3=openFileInput("password.txt");
     68             byte [] b3=new byte[1024];
     69             int i3=0;
     70             while ((i3=fis3.read(b3))>0)
     71             {
     72                 String pword1=new String(b3,0,i3);
     73 
     74                 pword+=pword1;
     75             }
     76             fis3.close();
     77 
     78         }
     79         catch (Exception e)
     80         {
     81 
     82         }
     83 
     84 
     85         String et_code=et_2.getText().toString();
     86         String et_pass=et_3.getText().toString();
     87         if (et_code.trim().length()==0||et_pass.trim().length()==0)
     88         {
     89             Toast.makeText(NCMActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
     90             return;
     91         }
     92 
     93         if (et_code ==null ||et_code.equals(code))
     94         {
     95             Toast.makeText(NCMActivity.this, "用户未注册", Toast.LENGTH_SHORT).show();
     96             return;
     97         }
     98 
     99 
    100 
    101         if (et_pass==null||et_pass.equals(pword))
    102         {
    103             Toast.makeText(NCMActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
    104 
    105             return;
    106         }
    107         else
    108         {
    109             Toast.makeText(NCMActivity.this,"用户登录成功",Toast.LENGTH_LONG).show();
    110 
    111             Intent intent=new Intent(this,NCTActivity.class);
    112 
    113             startActivity(intent);
    114 
    115             finish();
    116 
    117             }
    118 
    119 
    120     }
    121 }
    登录.java
      1 package com.example.wang.xuexi;
      2 
      3 import android.content.Intent;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.os.Bundle;
      6 import android.view.View;
      7 import android.widget.Button;
      8 import android.widget.EditText;
      9 import android.widget.Toast;
     10 
     11 import java.io.File;
     12 import java.io.FileOutputStream;
     13 import java.io.PrintStream;
     14 
     15 public class NCZActivity extends AppCompatActivity {
     16 
     17     EditText et_1;
     18 
     19     EditText et_2;
     20 
     21     EditText et_3;
     22 
     23     Button bt_1;
     24 
     25     @Override
     26     protected void onCreate(Bundle savedInstanceState) {
     27         super.onCreate(savedInstanceState);
     28         setContentView(R.layout.activity_ncz);
     29 
     30 
     31         et_1 = (EditText) findViewById(R.id.et_1);
     32         et_2 = (EditText) findViewById(R.id.et_2);
     33         et_3 = (EditText) findViewById(R.id.et_3);
     34 
     35         bt_1 = (Button) findViewById(R.id.bt_1);
     36 
     37         bt_1.setOnClickListener(new View.OnClickListener() {
     38             @Override
     39             public void onClick(View v) {
     40                 setResult(RESULT_CANCELED, null);
     41                 finish();
     42             }
     43         });
     44     }
     45 
     46 
     47     public void bt1_OnClick(View v) {
     48 
     49         String user_name = et_1.getText().toString();
     50 
     51         if (user_name == null || user_name.trim().length() == 0) {
     52             Toast.makeText(this, "请正确输入用户名", Toast.LENGTH_SHORT).show();
     53             return;
     54         }
     55 
     56         String user_code = et_2.getText().toString();
     57 
     58         if (user_code == null || user_code.trim().length() == 0) {
     59             Toast.makeText(this, "请正确输入用户代码", Toast.LENGTH_SHORT).show();
     60             return;
     61         }
     62 
     63         String pass_word = et_3.getText().toString();
     64 
     65         if (pass_word == null || pass_word.trim().length() == 0) {
     66             Toast.makeText(this, "请正确填写用户密码", Toast.LENGTH_SHORT).show();
     67             return;
     68         }
     69 
     70 
     71         try {
     72 
     73             FileOutputStream fos1 = openFileOutput("name.txt", MODE_PRIVATE);
     74             PrintStream ps1 = new PrintStream(fos1);
     75             ps1.println(user_name);
     76 
     77             FileOutputStream fos2 = openFileOutput("code.txt", MODE_PRIVATE);
     78             PrintStream ps2 = new PrintStream(fos2);
     79             ps2.println(user_code);
     80 
     81             FileOutputStream fos3 = openFileOutput("password.txt", MODE_PRIVATE);
     82             PrintStream ps3 = new PrintStream(fos1);
     83             ps3.println(pass_word);
     84 
     85             ps1.close();
     86             ps2.close();
     87             ps3.close();
     88 
     89             fos3.close();
     90 
     91             Toast.makeText(NCZActivity.this, "注册并保存成功", Toast.LENGTH_SHORT).show();
     92 
     93         } catch (Exception e) {
     94 
     95             Toast.makeText(NCZActivity.this, "注册并保存失败", Toast.LENGTH_SHORT).show();
     96         }
     97 
     98         Intent intent = new Intent(this,NCMActivity.class);
     99 
    100         startActivity(intent);
    101 
    102         finish();
    103 
    104     }
    105 
    106 
    107     }
    注册.xml
     1 package com.example.wang.xuexi;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.widget.TextView;
     6 
     7 import java.io.FileInputStream;
     8 
     9 public class NCTActivity extends AppCompatActivity {
    10 
    11     TextView tv_1;
    12     TextView tv_2;
    13     TextView tv_3;
    14     String name="";
    15     String code="";
    16     String pword="";
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_nct);
    22 
    23         tv_1=(TextView)findViewById(R.id.tv_1);
    24         tv_2=(TextView)findViewById(R.id.tv_2);
    25         tv_3=(TextView)findViewById(R.id.tv_3);
    26 
    27         try {
    28 
    29             FileInputStream fis1=openFileInput("name.txt");
    30             byte [] b1=new byte[1024];
    31             int i1=0;
    32             while ((i1=fis1.read(b1))>0)
    33             {
    34                 String name1=new String(b1,0,i1);
    35 
    36                 name+=name1;
    37             }
    38             tv_1.setText("用户名称:"+name);
    39             fis1.close();
    40 
    41             FileInputStream fis2=openFileInput("code.txt");
    42             byte [] b2=new byte[1024];
    43             int i2=0;
    44             while ((i2=fis2.read(b2))>0)
    45             {
    46                 String code1=new String(b2,0,i2);
    47 
    48                 code+=code1;
    49             }
    50             tv_2.setText("用户代码:"+code);
    51             fis2.close();
    52 
    53             FileInputStream fis3=openFileInput("password.txt");
    54             byte [] b3=new byte[1024];
    55             int i3=0;
    56             while ((i3=fis3.read(b3))>0)
    57             {
    58                 String pword1=new String(b3,0,i3);
    59 
    60                 pword+=pword1;
    61             }
    62             tv_3.setText("用户密码:"+pword);
    63             fis3.close();
    64 
    65         }
    66         catch (Exception e)
    67         {
    68 
    69 
    70         }
    71 }}
    显示.java

  • 相关阅读:
    LeetCode 382. Linked List Random Node
    LeetCode 398. Random Pick Index
    LeetCode 1002. Find Common Characters
    LeetCode 498. Diagonal Traverse
    LeetCode 825. Friends Of Appropriate Ages
    LeetCode 824. Goat Latin
    LeetCode 896. Monotonic Array
    LeetCode 987. Vertical Order Traversal of a Binary Tree
    LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
    LeetCode 636. Exclusive Time of Functions
  • 原文地址:https://www.cnblogs.com/arxk/p/5533121.html
Copyright © 2011-2022 走看看