package com.example.charp1; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click1(View view) { EditText editText1 = (EditText) findViewById(R.id.et1); EditText editText2 = (EditText) findViewById(R.id.et2); String filename = "data.txt"; String context = editText1.getText().toString(); FileOutputStream fos = null; try { fos = openFileOutput(filename, MODE_PRIVATE); fos.write(context.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } public void click2(View view) { EditText editText2 = (EditText) findViewById(R.id.et2); String context = ""; FileInputStream fis = null; try { fis = openFileInput("data.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); context = new String(buffer); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } editText2.setText(context); } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt1); findViewById(R.id.bt2); EditText editText1=findViewById(R.id.et1); EditText editText2=findViewById(R.id.et2); } public void click1(View view){ EditText editText1=findViewById(R.id.et1); EditText editText2=findViewById(R.id.et2); String fileName="data.txt"; String context=editText1.getText().toString(); FileOutputStream fos=null; try { fos=openFileOutput(fileName,MODE_PRIVATE); fos.write(context.getBytes()); }catch(Exception e){ e.printStackTrace(); }finally { if (fos!=null){ try { fos.close(); }catch(IOException e){ e.printStackTrace(); } } } Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); } public void click2(View view){ EditText editText2=findViewById(R.id.et2); String context=""; FileInputStream fis=null; try { fis=openFileInput("data.txt"); byte[]buffer=new byte[fis.available()]; fis.read(buffer); context=new String(buffer); }catch (Exception e){ e.printStackTrace(); }finally { if (fis!=null){ try{ fis.close(); }catch(IOException e){ e.printStackTrace(); } } } editText2.setText(context); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:textSize="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#000"/> <EditText android:id="@+id/et1" android:layout_width="300dp" android:layout_height="wrap_content" android:hint="请输入用户名" android:layout_toRightOf="@id/tv1"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="密码" android:textColor="#000" android:textSize="20dp" android:layout_marginTop="10dp" android:layout_below="@id/et1"/> <EditText android:id="@+id/et2" android:layout_width="300dp" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_below="@id/et1" android:layout_toRightOf="@id/tv2"/> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textColor="#FFFFFF" android:background="#000" android:layout_below="@id/et2" android:layout_marginLeft="280dp"/> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:layout_below="@id/et2" android:layout_marginTop="10dp" android:layout_marginLeft="50dp"/> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" android:layout_below="@id/et2" android:layout_toRightOf="@id/cb1" android:layout_marginTop="10dp" android:layout_marginLeft="20dp"/> </RelativeLayout>
package com.example.qq; import android.content.Context; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FileSaveQQ { public static boolean saveUserInfo(Context context, String account, String password ) { FileOutputStream fos = null; try{ fos =context.openFileOutput("data.txt",Context.MODE_PRIVATE); fos.write((account+":"+password).getBytes()); return true; }catch (Exception e){ e.printStackTrace(); return false; }finally { try{ if (fos !=null){ fos.close(); } }catch (IOException e){ e.printStackTrace(); } } } public static Map<String, String> getUserInfo(Context context){ String content=""; FileInputStream fis= null; try { fis=context.openFileInput("data.txt"); byte[] buffer =new byte[fis.available()]; fis.read(buffer); content=new String(buffer); Map<String,String> userMap = new HashMap<String, String>(); String[] infos = content.split(":"); userMap.put("account",infos[0]); userMap.put("password",infos[1]); return userMap; }catch (Exception e){ e.printStackTrace(); return null; } finally { try { if (fis!=null){ fis.close(); } }catch (IOException e){ e.printStackTrace(); } } } }
package com.example.myapplication; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class FileSaveQQ { public static boolean saveUserInfo(Context context,String account,String password ){ SharedPreferences sp=context.getSharedPreferences("data", context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.putString("Username", account); editor.putString("pwd", password); editor.commit(); return true; } public static Map<String, String> getUserInfo(Context context){ SharedPreferences sp=context.getSharedPreferences("data", context.MODE_PRIVATE); String account=sp.getString("Username", null); String password=sp.getString("pwd", null); Map<String, String> userMap=new HashMap<String,String>(); userMap.put("account", account); userMap.put("password", password); return userMap; } }