记账本小程序7天开发记录(第五天)

时间:2019-02-22 18:28:12   收藏:0   阅读:206

开发家庭记账本小程序必然会用到数据的存储,所以今天主要学习Android开发的数据存储。

##Android开发的数据存储

  1. 应用程序只可以把数据存储在自己私有的文件夹里, data/data/<包名>/文件名...
    1. 上下文:应用程序运行的环境
    2. api获取目标目录:
      • this.getFileDir()===>data/data/<包名>/files     //保存重要的配置信息
  1. 应用程序可以把数据存储在外存储卡
File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
        if(file.exists()&&file.length()>0) {
            try {
                FileInputStream fis = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String info = br.readLine();
                String qq = info.split("##")[0];
                String pwd = info.split("##")[1];
                et_qq.setText(qq);
                et_password.setText(pwd);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
  1. 应用程序在data/data/包名/目录下创建的文件都是私有的,其他应用程序是不可以访问的。
  2. 默认生成的文件都是私有的
//生成公开权限的文件
    public void getpublicFile(View view) {
        try {
            FileOutputStream fos = openFileOutput("public.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_READABLE);
            fos.write("private".getBytes());
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  1. chmod:change mode       Linux下更改文件访问的权限
  1. 声明 sharedperference  //sp = this.getSharedPreferences("config", 0);
  2. 获取到一个参数
  3. 获取编辑器  //Editor editor = sp.edit();
  4. editor.putString(key,value);  //  editor.putInt();  //  editor.putDouble();
  5. editor.commit();保存数据完毕,必须记得调用commit的方法
  6. 获取数据 sp.getString(key,dafvalue);  //  sp.getInt();  //  sp.getDouble();
public class MainActivity extends Activity {

    
    private static String MainActivity;
    private static final String TAG = MainActivity;
    private EditText et_qq;
    private EditText et_password;
    private CheckBox cb_remember;
    //1.声明sp
    private SharedPreferences sp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        et_qq = findViewById(R.id.et_qq);
        et_password = findViewById(R.id.et_password);
        cb_remember = findViewById(R.id.cb_remember);
        //2.获取到一个参数
        sp = this.getSharedPreferences("config", 0);
        String qq = sp.getString("qq", "");    
        String pwd = sp.getString("pwd", "");
        et_qq.setText(qq);
        et_password.setText(pwd);
    }
    
    public void login(View view) {
        String qq = et_qq.getText().toString().trim();
        String pwd = et_password.getText().toString().trim();
        if(TextUtils.isEmpty(qq)||TextUtils.isEmpty(pwd)){
            Toast.makeText(this, "用户名或密码不能为空", 0).show();
            return;
        }
        if(cb_remember.isChecked()) {
            //CheckBox被勾选时记住密码
            Log.i(TAG,"记住密码");
            //3.得到sp文件的编辑器
            Editor editor = sp.edit();
            editor.putString("qq",qq);
            editor.putString("pwd",pwd);
            //4.保存数据完毕,必须记得调用commit的方法
            editor.commit();
        }else {
            //不需要记住密码
            Log.i(TAG,"不需要记住密码");
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!