把数据存储到文件(login案例) android 下的数据存储
1.写布局
LinearLayout + RelativeLayout
2.写业务逻辑
a.找到相应控件
b.设置按钮的点击事件
c.在onclick方法中,获取用户输入的用户名密码和是否记住密码
d.判断用户名密码是否为空,不为空请求服务器(省略,默认请求成功)
e.判断是否记住密码,如果记住,将用户名密码保存本地。????
f.回显用户名密码 ??
//通过context对象获取私有目录,/data/data/packagename/filse
context.getFilesDir().getPath()
package xzit.com.logincase;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import java.util.Map;
import xzit.com.logincase.util.UserInfoUtil;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_username, et_password;
private CheckBox cb_rem;
private Button bt_login;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
//a.找到相应控件
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_rem = (CheckBox) findViewById(R.id.cb_rem);
bt_login = (Button) findViewById(R.id.bt_login);
//b.设置按钮的点击事件
bt_login.setOnClickListener(this);
//f.回显用户名密码 ??
Map<String, String> map = UserInfoUtil.getUserInfo(mContext);//获取用户名密码
if(map !=null){
String username=map.get("username");
String password=map.get("password");
System.out.println(username+password);
et_username.setText(username);//设置用户名
et_password.setText(password);
cb_rem.setChecked(true);
}
}
private void login() {
//c.在onclick方法中,获取用户输入的用户名密码和是否记住密码
String username=et_username.getText().toString().trim();//.trim();这个的意思是去掉空格
String password=et_password.getText().toString().trim();
boolean isrem=cb_rem.isChecked();
//d.判断用户名密码是否为空,不为空请求服务器(省略,默认请求成功)
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
Toast.makeText(mContext,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
return ;//因为没有必要往下走了,所以要return一下
}
//请求服务器。
//e.判断是否记住密码,如果记住,将用户名密码保存本地。????
if(isrem){
boolean result= UserInfoUtil.saveUserInfo(mContext,username, password);
if(result){
Toast.makeText(mContext,"用户名密码保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,"用户名密码保存失败",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mContext,"无需保存",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
login();
break;
default:
break;
}
}
}
package xzit.com.logincase.util;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2016/6/6.
*/
public class UserInfoUtil {
public static boolean saveUserInfo(Context context,String username, String password) {
//保存用户名和密码
//要保存在文件里面,因为如果保存在内存里面,有可能被清除掉了
//文件保存的路径
try {
String userInfo=username+"##"+password;//封装用户名,密码
//通过Context对象获取私有目录的一个路径
String path=context.getFilesDir().getPath();//指定保存的路径
System.out.println("..............."+path);
File file = new File(path, "userInfo.txt");//创建file
//往文件里面写东西就用FileOutputStream
FileOutputStream fileOutputStream = new FileOutputStream(file);//创建文件写入流
fileOutputStream.write(userInfo.getBytes());//最后将用户名,密码写入文件
fileOutputStream.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
//获取用户名,密码
public static Map<String,String> getUserInfo(Context context){
try{
String path=context.getFilesDir().getPath();
File file = new File(path, "userInfo.txt");
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String readLine = bufferedReader.readLine();//读取一行
String[] split = readLine.split("##");
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username",split[0]);
hashMap.put("password",split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}