效果图:
1.activity_main.xml
<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/ed_name1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" /> <EditText android:id="@+id/ed_pass1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> <CheckBox android:id="@+id/ed_cba" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住账号和密码"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="login1" android:text="登陆"/> </LinearLayout>2.MainActivity
package com.liyulei.memoryread; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; public class TwoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); } //外存卡存储信息 public void login1(View view ) { //拿到输入框的内容 EditText ed_name1 = (EditText) findViewById(R.id.ed_name1); EditText ed_pass1 = (EditText) findViewById(R.id.ed_pass1); String name1 = ed_name1.getText().toString(); String pass1 = ed_pass1.getText().toString(); //判断checkbox是否选中 CheckBox ed_cba = (CheckBox) findViewById(R.id.ed_cba); if (ed_cba.isChecked()) { //检测SD卡是否可用 //MEDIA_MOUNTED:sd卡可用 //MEDIA_REMOVED:sd卡不存在 //MEDIA_CHECKING:sd卡正在遍历 //MEDIA_UNMOUNTED:sd卡存在,但是没有挂载 //MEDIA_MOUNTED_READ_ONLY:sd卡可用,但是只读 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //把账号密码保存到本地(第一种方法) //File file = new File("sdcard/info.text"); //另一种方法获取 File file = new File(Environment.getExternalStorageDirectory(), "info.text"); try { FileOutputStream fileOutputStream = new FileOutputStream(file); //把账号密码写入本地 fileOutputStream.write((name1 + "&&" + pass1).getBytes()); //关闭资源 fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } }else { Toast toast = Toast.makeText(this,"sd卡不可用呦亲",Toast.LENGTH_LONG); toast.show(); } Toast toast = Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG); toast.show(); } } //外存卡读取信息 public void Account(){ //读取文件,回显数据 // File file= new File("sdcard/info.text"); //另一种方法获取读取文件,回显数据 File file = new File(Environment.getExternalStorageDirectory(),"info.text"); //exists()测试此抽象路径名定义的文件或目录是否存在 if(file.exists()){ try { FileInputStream fs= new FileInputStream(file); //把字节流转化成字符流 BufferedReader bf = new BufferedReader(new InputStreamReader(fs)); //读取文件中的文本 String text = bf.readLine(); String s[] = text.split("&&"); //给输入框设置显示内容 EditText ed_cba = (EditText)findViewById(R.id.ed_cba); ed_cba.setText(s[0]); } catch (Exception e) { e.printStackTrace(); } } } }