11.12
1. 用内部存储实现文件写入和读取功能
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入你想写入的内容:" android:layout_margin="20dp"/> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入" android:layout_margin="5dp" android:onClick="click1"/> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="显示读取的内容" android:layout_margin="20dp"/> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取" android:layout_margin="5dp" android:onClick="click2"/> </LinearLayout>
package com.example.zy; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileNotFoundException; 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.bt_1); findViewById(R.id.bt_2); EditText editText1=findViewById(R.id.et_1); EditText editText2=findViewById(R.id.et_2); } public void click1(View view){ EditText editText1=findViewById(R.id.et_1); EditText editText2=findViewById(R.id.et_2); String fileName="data.txt"; String content=editText1.getText().toString(); FileOutputStream fos=null; try { fos=openFileOutput(fileName,MODE_PRIVATE); fos.write(content.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { if (fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show(); } public void click2(View view){ EditText editText2=findViewById(R.id.et_2); String content=""; FileInputStream fis=null; try { fis=openFileInput("data.txt"); byte[] buffer=new byte[fis.available()]; fis.read(buffer); content=new String(buffer); } catch (Exception e) { e.printStackTrace(); } finally { if (fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } Toast.makeText(MainActivity.this,"读取成功",Toast.LENGTH_SHORT).show(); editText2.setText(content); } }
2.使用sharedpreference实现记住密码功能
<?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" tools:context=".MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号:" android:textSize="25sp" android:textColor="#000" android:layout_marginTop="40dp" android:layout_marginLeft="10dp"/> <EditText android:id="@+id/et_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名:" android:layout_marginTop="40dp" android:textSize="25sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textSize="25sp" android:textColor="#000" android:layout_marginTop="15dp" android:layout_marginLeft="10dp"/> <EditText android:id="@+id/et_4" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="15dp" android:textSize="25sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:textSize="20sp" android:layout_marginTop="15dp"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" android:textSize="20sp" android:layout_marginTop="15dp"/> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆" android:textSize="20sp" android:layout_marginTop="15dp" android:layout_marginLeft="35dp" android:onClick="clickQQ" tools:ignore="OnClick" /> </LinearLayout> </LinearLayout>
package com.example.dierti; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText yonghuming,mima; private CheckBox box; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); yonghuming=findViewById(R.id.et_3); mima=findViewById(R.id.et_4); box=findViewById(R.id.cb_1); load(); } public void clickQQ(View view){ String yhm=yonghuming.getText().toString(); String mm=mima.getText().toString(); if (box.isChecked()){ SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE); SharedPreferences.Editor et=sp.edit(); et.putString("YHM",yhm); et.putString("MM",mm); et.commit(); Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show(); } } private void load() { SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE); String yhmString=sp.getString("YHM",""); String mmString=sp.getString("MM",""); yonghuming.setText(yhmString); mima.setText(mmString); } }