使用SharedPreferences
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=getSharedPreferences("config",0);
edit_name=(EditText) findViewById(R.id.name);
edit_password=(EditText) findViewById(R.id.password);
edit_remember=(CheckBox) findViewById(R.id.remember);
//把config.xml文件中我们关心的数据取出来 然后显示到editText上
String name=sp.getString("name","");
String password=sp.getString("password","");
edit_name.setText(name);
edit_password.setText(password);
}
public void login(View v){
String name=edit_name.getText().toString().trim();
String password=edit_password.getText().toString().trim();
//判断是否为空
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
Toast.makeText(MainActivity.this,"用户名或密码为空",1).show();//1是时长
}else{
System.out.println("连接服务器,进行登入");
if (edit_remember.isChecked()){
//使用SharedPreferences去保存数据 拿到sp的实例
// SharedPreferences sp=getSharedPreferences("config",0);
//获取sp的编辑器
Editor edit=sp.edit();
edit.putString("name",name);
edit.putString("password",password);
//记得提交
edit.commit();
}
}
}
public static Map<String ,String> readInfo(Context context){
try{//定义Map
Map<String,String> maps=new HashMap<String,String>();
FileInputStream fis= context.openFileInput("infoo.txt");
BufferedReader bufr=new BufferedReader(new InputStreamReader(fis));
String content= bufr.readLine();//读取数据
//切割字符串 封装到map集合中
String[] splits=content.split("##");
String name=splits[0];
String password =splits[1];
//把name 和 password 放入map中
maps.put("name",name);
maps.put("password",password);
fis.close();
return maps;
}catch(Exception e){
e.printStackTrace();
return null;
}
}