xml文件
Utils
package com.example.android22filelogin;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.text.TextUtils;public class Utils { public static boolean saveUserInfo(String username,String pwd){ String data=username+"##"+pwd; String path="/data/data/com.example.android22filelogin/data.txt"; try { FileOutputStream out=new FileOutputStream(path); out.write(data.getBytes()); out.flush(); out.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public static MapgetUserInfo(){ String path="/data/data/com.example.android22filelogin/data.txt"; try { BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path))); String data=reader.readLine(); if(!TextUtils.isEmpty(data)) { String [] datas=data.split("##"); Map userinfo=new HashMap (); userinfo.put("number", datas[0]); userinfo.put("pwd", datas[1]); return userinfo; } } catch (Exception e) { e.printStackTrace(); } return null; } }
activity
public class MainActivity extends Activity implements OnClickListener { private EditText qqnum,pwd; private CheckBox rem; private Button but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qqnum=(EditText)findViewById(R.id.qqnum); pwd=(EditText)findViewById(R.id.pass); rem=(CheckBox)findViewById(R.id.rem); but=(Button)findViewById(R.id.Login); but.setOnClickListener(this); //回显数据 Mapuserinfo=Utils.getUserInfo(); if(userinfo!=null) { qqnum.setText(userinfo.get("number")); pwd.setText(userinfo.get("pwd")); } } @Override public void onClick(View v) { //记住号码和密码 String num=qqnum.getText().toString(); String password=pwd.getText().toString(); if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password)) { Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show(); return; } //判断是否记住密码 if(rem.isChecked()) { boolean isSuccess=Utils.saveUserInfo(num, password); Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show(); } //登录成功 }}