Android day_3 (登录案例 SharedPreferences  的使用)

 

登录案例:

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卡的可用空间和总空间

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值