Android(安卓)day_3 (登录案例 SharedPreferences 的使用)
16lz
2021-01-24
登录案例:
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(上下文)获取FileInputStreamFileInputStream fis=context.openFileInput("infoo.txt");--参数为要读取的文件
4)
Map的使用创建Map Map masps=new HashMap();向Map中写入数据 String[] splits=content.split("##"); --根据#拆分字符串 String name = splits[0]; String pwd =splits[1]; masps.put("name",name); masps.put("pwd",pwd); --把数组中的数据写入Map,名字对应数据从Map中读取数据 Map maps=UserinfoUtils.readInof(MainActivity.this ); String name=maps.get("name"); String pwd=maps.get("pwd"); --把数据从Map中读出,.get(“给数据起的名字”)
5)把数据存入sd卡中
给用户添加sd卡的权限
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卡的可用空间和总空间
更多相关文章
- Android(安卓)jni 编程(参数的传递,成员,方法的)相互访问
- Android(安卓)保存图片到SQLite
- (Android) ContentProvider 实例
- android 利用pull解析xml数据
- Android:week 11总结 内容提供者、数据库
- 下拉菜单中自动亮度调节&P 上背光调节
- Android(安卓)数据库SQLite的使用简单Demo
- Android(安卓)-- 跨应用启动Service
- android http请求访问接口的封装