package com.hanqi.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 Main2Activity extends AppCompatActivity { EditText user_name; EditText user_password; Button bt_1; Button bt_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); 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_username=user_name.getText().toString(); String input_password=user_password.getText().toString(); if (input_username.trim().length()==0||input_password.trim().length()==0) { Toast.makeText(Main2Activity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show(); }else { Intent intent=new Intent(Main2Activity.this,Main22Activity.class); startActivity(intent); try { File file=getFilesDir(); String path=file.getAbsolutePath(); FileOutputStream fos=openFileOutput("test.txt", MODE_APPEND); PrintStream ps=new PrintStream(fos); ps.print(input_username); ps.close(); fos.close(); FileOutputStream fos1=openFileOutput("tes.txt", MODE_APPEND); PrintStream ps1=new PrintStream(fos1); ps1.print(input_password); ps1.close(); fos1.close(); Toast.makeText(Main2Activity.this, "保存成功", Toast.LENGTH_SHORT).show(); }catch (Exception e) { e.printStackTrace(); } } } }); } }
package com.hanqi.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import java.io.FileInputStream; public class Main22Activity extends AppCompatActivity { EditText file_name; EditText file_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main22); file_name=(EditText)findViewById(R.id.file_name); file_password=(EditText)findViewById(R.id.file_password); try { FileInputStream fis =openFileInput("test.txt"); byte[] bytes =new byte[1024]; int i=0; String str1 = ""; while ((i=fis.read(bytes))>0) { String str = new String(bytes,0,i); str1+=str; } fis.close(); file_name.setText(str1); int ii=0; FileInputStream fis1 =openFileInput("tes.txt"); byte[] bytes1 =new byte[1024]; String str2 = ""; while ((ii=fis1.read(bytes1))>0) { String str = new String(bytes1,0,ii); str2+=str; } fis1.close(); file_password.setText(str2); }catch (Exception e) { e.printStackTrace(); } } }
<?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.myapplication.Main2Activity" 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="用户密码"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_1" android:text="登陆" /> </LinearLayout>
<?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.myapplication.Main22Activity" 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>