登录案例:
1)SharedPreferences 的使用
通过上下文拿到SharedPreferences实例
SharedPreferences sp=getSharedPreferences("sp",0);
存入数据:
//获取sp的编辑器
SharedPreferences.Editor edit=sp.edit();
edit.putString("name",name);
edit.putString("pwd",pwd);
//提交编辑器
edit.commit();
读取数据:
String name=sp.getString("name","");
String pwd=sp.getString("pwd","");
//第一个参数为写入时定义的键,第二个为通过键找不到时的默认值
注意: 写入时的数据类型和读取时的数据类型要对应
2)☆☆☆☆给CheckBox控件设置是否被选中事件
cb_ischeck1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
//判断CheckBox是否被选中
if (cb_ischeck1.isChecked()) {
/* 设定EditText的内容为可见的 */
et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
/* 设定EditText的内容为隐藏的 */
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
3)☆☆☆☆使用上下文快速获取文件的输入输出流。
String path=context.getFilesDir().getPath();获取地址
File file=new File(path,"info.txt");
//通过Context(上下文)获取FileOutputStream
FileOutputStream fos=context.openFileOutput("infoo.txt",0);
---第一个参数为新建的文件名第二个为模式0代表私有模式
//通过Context(上下文)获取FileInputStream
FileInputStream fis=context.openFileInput("infoo.txt");
--参数为要读取的文件
4)
Map的使用
创建Map
Map<String,String> masps=new HashMap<String,String>();
向Map中写入数据
String[] splits=content.split("##"); --根据#拆分字符串
String name = splits[0];
String pwd =splits[1];
masps.put("name",name);
masps.put("pwd",pwd); --把数组中的数据写入Map,名字对应数据
从Map中读取数据
Map<String,String> maps=UserinfoUtils.readInof(MainActivity.this );
String name=maps.get("name");
String pwd=maps.get("pwd"); --把数据从Map中读出,.get(“给数据起的名字”)
5)把数据存入sd卡中
给用户添加sd卡的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
6)获取sd卡的路径
String sdPath= Environment.getExternalStorageDirectory().getPath();
File file = new File(sdPath,"ha.txt");
7)判断sd卡是否可用
Environment.getExternalStorageState() 获取sd卡当前状态
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
Toast.makeText(MainActivity.this,"sd卡可用",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"sd卡不可用",Toast.LENGTH_LONG).show();
}
8)获取sd卡的可用空间和总空间