用户的注册信息存储到文件里

登录界面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.example.wang.xuexi.NCMActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名称"
        android:id="@+id/et_2"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="numberPassword"
        android:layout_below="@id/et_2"
        android:id="@+id/et_3"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_3">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:id="@+id/bt_1"
            android:onClick="bt1_OnClick"
            android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="注册"
            android:id="@+id/bt_2"
            android:onClick="bt2_OnClick"
            android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>
View Code

注册界面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.example.wang.xuexi.NCZActivity"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名称"
        android:id="@+id/et_1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户代码"
        android:id="@+id/et_2"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:id="@+id/et_3"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="取消"
            android:id="@+id/bt_1"
            android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="确定"
            android:id="@+id/bt_2"
            android:layout_weight="1"
            android:onClick="bt1_OnClick"/>
    </LinearLayout>

</LinearLayout>
View Code

显示界面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.example.wang.xuexi.NCTActivity"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/tv_1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/tv_2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/tv_3"/>

</LinearLayout>

显示界面.xml
View Code

登录Activity代码:

package com.example.wang.xuexi;

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.FileInputStream;

public class NCMActivity extends AppCompatActivity {

    EditText et_2;

    EditText et_3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ncm);

        et_2=(EditText)findViewById(R.id.et_2);
        et_3=(EditText)findViewById(R.id.et_3);
    }

    public void bt2_OnClick(View v)
    {
        Intent intent=new Intent(this,NCZActivity.class);

        startActivity(intent);
    }



    public void bt1_OnClick(View v)
    {
        String name="";
        String code="";
        String pword="";

        try {

            FileInputStream fis1=openFileInput("name.txt");
            byte [] b1=new byte[1024];
            int i1=0;
            while ((i1=fis1.read(b1))>0)
            {
                String name1=new String(b1,0,i1);

                name+=name1;
            }
            fis1.close();

            FileInputStream fis2=openFileInput("code.txt");
            byte [] b2=new byte[1024];
            int i2=0;
            while ((i2=fis2.read(b2))>0)
            {
                String code1=new String(b2,0,i2);

                code+=code1;
            }
            fis2.close();

            FileInputStream fis3=openFileInput("password.txt");
            byte [] b3=new byte[1024];
            int i3=0;
            while ((i3=fis3.read(b3))>0)
            {
                String pword1=new String(b3,0,i3);

                pword+=pword1;
            }
            fis3.close();

        }
        catch (Exception e)
        {

        }


        String et_code=et_2.getText().toString();
        String et_pass=et_3.getText().toString();
        if (et_code.trim().length()==0||et_pass.trim().length()==0)
        {
            Toast.makeText(NCMActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (et_code ==null ||et_code.equals(code))
        {
            Toast.makeText(NCMActivity.this, "用户未注册", Toast.LENGTH_SHORT).show();
            return;
        }



        if (et_pass==null||et_pass.equals(pword))
        {
            Toast.makeText(NCMActivity.this, "密码错误", Toast.LENGTH_SHORT).show();

            return;
        }
        else
        {
            Toast.makeText(NCMActivity.this,"用户登录成功",Toast.LENGTH_LONG).show();

            Intent intent=new Intent(this,NCTActivity.class);

            startActivity(intent);

            finish();

            }


    }
}
View Code

注册Activity代码:

package com.example.wang.xuexi;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class NCZActivity extends AppCompatActivity {

    EditText et_1;

    EditText et_2;

    EditText et_3;

    Button bt_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ncz);


        et_1 = (EditText) findViewById(R.id.et_1);
        et_2 = (EditText) findViewById(R.id.et_2);
        et_3 = (EditText) findViewById(R.id.et_3);

        bt_1 = (Button) findViewById(R.id.bt_1);

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED, null);
                finish();
            }
        });
    }


    public void bt1_OnClick(View v) {

        String user_name = et_1.getText().toString();

        if (user_name == null || user_name.trim().length() == 0) {
            Toast.makeText(this, "请正确输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String user_code = et_2.getText().toString();

        if (user_code == null || user_code.trim().length() == 0) {
            Toast.makeText(this, "请正确输入用户代码", Toast.LENGTH_SHORT).show();
            return;
        }

        String pass_word = et_3.getText().toString();

        if (pass_word == null || pass_word.trim().length() == 0) {
            Toast.makeText(this, "请正确填写用户密码", Toast.LENGTH_SHORT).show();
            return;
        }


        try {

            FileOutputStream fos1 = openFileOutput("name.txt", MODE_PRIVATE);
            PrintStream ps1 = new PrintStream(fos1);
            ps1.println(user_name);

            FileOutputStream fos2 = openFileOutput("code.txt", MODE_PRIVATE);
            PrintStream ps2 = new PrintStream(fos2);
            ps2.println(user_code);

            FileOutputStream fos3 = openFileOutput("password.txt", MODE_PRIVATE);
            PrintStream ps3 = new PrintStream(fos1);
            ps3.println(pass_word);

            ps1.close();
            ps2.close();
            ps3.close();

            fos3.close();

            Toast.makeText(NCZActivity.this, "注册并保存成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {

            Toast.makeText(NCZActivity.this, "注册并保存失败", Toast.LENGTH_SHORT).show();
        }

        Intent intent = new Intent(this,NCMActivity.class);

        startActivity(intent);

        finish();

    }


    }
View Code

显示Activity代码:

package com.example.wang.xuexi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.FileInputStream;

public class NCTActivity extends AppCompatActivity {

    TextView tv_1;
    TextView tv_2;
    TextView tv_3;
    String name="";
    String code="";
    String pword="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nct);

        tv_1=(TextView)findViewById(R.id.tv_1);
        tv_2=(TextView)findViewById(R.id.tv_2);
        tv_3=(TextView)findViewById(R.id.tv_3);

        try {

            FileInputStream fis1=openFileInput("name.txt");
            byte [] b1=new byte[1024];
            int i1=0;
            while ((i1=fis1.read(b1))>0)
            {
                String name1=new String(b1,0,i1);

                name+=name1;
            }
            tv_1.setText("用户名称:"+name);
            fis1.close();

            FileInputStream fis2=openFileInput("code.txt");
            byte [] b2=new byte[1024];
            int i2=0;
            while ((i2=fis2.read(b2))>0)
            {
                String code1=new String(b2,0,i2);

                code+=code1;
            }
            tv_2.setText("用户代码:"+code);
            fis2.close();

            FileInputStream fis3=openFileInput("password.txt");
            byte [] b3=new byte[1024];
            int i3=0;
            while ((i3=fis3.read(b3))>0)
            {
                String pword1=new String(b3,0,i3);

                pword+=pword1;
            }
            tv_3.setText("用户密码:"+pword);
            fis3.close();

        }
        catch (Exception e)
        {


        }
}}
View Code
原文地址:https://www.cnblogs.com/beens/p/5533425.html