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

package com.example.wang.myapplication;

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

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Main3Activity extends AppCompatActivity {
    EditText user_name;
    EditText user_password;
    Button bt_1;
    Button bt_2;
    String return_name;
    String return_password;

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

        user_name=(EditText)findViewById(R.id.user_name);
        user_password=(EditText)findViewById(R.id.user_password);
        bt_1=(Button)findViewById(R.id.bt_1);
        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input_name=user_name.getText().toString();
                String input_password=user_password.getText().toString();

                if (input_name.trim().length()==0||input_password.trim().length()==0)
                {
                    Toast.makeText(Main3Activity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                else if(input_name.equals(return_name))
                       {
                           if (input_password.equals(return_password))
                           {
                               Intent intent=new Intent(Main3Activity.this,Main32Activity.class);
                               startActivity(intent);

                               try {
                                   FileOutputStream fos=openFileOutput("test.txt",MODE_PRIVATE);
                                   PrintStream ps=new PrintStream(fos);
                                   ps.print(input_name);
                                   ps.close();
                                   fos.close();

                                   FileOutputStream fos1=openFileOutput("test1.txt",MODE_PRIVATE);
                                   PrintStream ps1=new PrintStream(fos1);
                                   ps1.print(input_password);
                                   ps1.close();
                                   fos1.close();

                               }
                               catch (IOException e)
                               {
                                   e.printStackTrace();
                               }

                           }
                           else
                           {
                               Toast.makeText(Main3Activity.this, "密码错误", Toast.LENGTH_SHORT).show();
                           }
                       }

                else
                {
                    Toast.makeText(Main3Activity.this, "用户未注册", Toast.LENGTH_SHORT).show();
                }
            }
        });


        bt_2=(Button)findViewById(R.id.bt_2);
        bt_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Main3Activity.this,ZhuceActivity.class);

                startActivityForResult(intent,1);
            }


        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==1)
        {
            if (resultCode==RESULT_OK)
            {
                return_name=data.getStringExtra("username");
                return_password=data.getStringExtra("userpassword");

                Log.e("TAG","return_name="+return_name);

                Log.e("TAG","return_password="+return_password);
            }
        }
    }
}
Main3Activity
package com.example.wang.myapplication;

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;

public class ZhuceActivity extends AppCompatActivity {

    EditText username;
    EditText userpassword;
    Button bt_11;
    Button bt_12;

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

        username=(EditText)findViewById(R.id.username);
        userpassword=(EditText)findViewById(R.id.userpassword);
        bt_11=(Button)findViewById(R.id.bt_11);
        bt_11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=username.getText().toString();
                String password=userpassword.getText().toString();
                if (name.trim().length()==0||password.trim().length()==0)
                {
                    Toast.makeText(ZhuceActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Intent intent=new Intent();

                    intent.putExtra("username",name);
                    intent.putExtra("userpassword",password);

                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });



        bt_12=(Button)findViewById(R.id.bt_12);
        bt_12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
ZhuceActivity
package com.example.wang.myapplication;

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

import java.io.FileInputStream;

public class Main32Activity extends AppCompatActivity {

    EditText file_name;
    EditText file_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main32);

        file_name=(EditText)findViewById(R.id.file_name);
        file_password=(EditText)findViewById(R.id.file_password);


        try {
            FileInputStream fis = openFileInput("test.txt");
            byte[] b=new byte[1024];
            int i=0;

            String str1="";

            while((i=fis.read(b))>0)
            {
                String str=new String(b,0,i);

                str1+=str;
            }

            fis.close();
            file_name.setText(str1);

            FileInputStream fis1 = openFileInput("test1.txt");
            byte[] b1=new byte[1024];
            int i1=0;

            String str11="";

            while((i1=fis1.read(b1))>0)
            {
                String str=new String(b1,0,i1);

                str11+=str;
            }

            fis1.close();
            file_password.setText(str11);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }



    }
}
Main32Activity
<?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.myapplication.ZhuceActivity"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:id="@+id/username"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户密码"
        android:id="@+id/userpassword"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/bt_11"
            android:text="确定"
            android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/bt_12"
            android:text="取消"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>
activity_zhuce
<?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.myapplication.Main32Activity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <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/file_name"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <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/file_password"/>
    </LinearLayout>



</LinearLayout>
activity_main32
<?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.myapplication.Main3Activity"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/user_name"
        android:hint="用户名"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/user_password"
        android:hint="用户密码"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/bt_1"
            android:text="登陆"
            android:layout_weight="1"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/bt_2"
            android:text="注册"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>
activity_main3

原文地址:https://www.cnblogs.com/wangchuanqi/p/5533410.html