SettingsFragment

由于密码是以List形式存储的,可以用SharedPreferences的方法储存在本地,比较方便(不过安全性就不那么好了),本来SharedPreferences应该是可以跨Activity读取的,但是一直都报空对象的错误,然后了解了一下 getPreferences与getDefaultSharedPreferences的区别 ,参考 https://blog.csdn.net/w47_csdn/article/details/51766401 ,换成getDefaultSharedPreferences就不会报错了

储存密码:

private void  Save_Password(List<String> arrayList, String s){
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("size", arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            edit.remove(s+i);
            edit.putString(s + i, arrayList.get(i));
        }
        edit.apply();
    }

MainActivity

读取密码以及查询密码

private boolean Search_password(List<String> passwords,String s){
        return passwords.contains(s);
    }
private void Get_Password(List<String> arrayList){
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
        int size=sp.getInt("size",0);
        arrayList.clear();
        for(int i=0;i<size;i++) {
            arrayList.add(sp.getString("num_password"+i,""));
        }
    }

输入密码对话框

private void show_Password_Dialog(){

        final AlertDialog.Builder bottom_dialog=new AlertDialog.Builder(this);
        final View dialog_view=LayoutInflater.from(this).inflate(R.layout.password_num_dialog,null);
        bottom_dialog.setView(dialog_view);
        bottom_dialog.setCancelable(false);
        bottom_dialog.setTitle("输入密码");
        final AlertDialog alertDialog=bottom_dialog.show();

        final EditText password_edit=dialog_view.findViewById(R.id.password_num_edit);
        Button confirm_btn=dialog_view.findViewById(R.id.password_num_confirm);
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=password_edit.getText().toString();
                if (Search_password(passwords,password)){
                    alertDialog.dismiss();
                }else {
                    Toast.makeText(MainActivity.this, "请输入正确密码", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

最后的效果

至此,这个记账APP基本就制作完成了,历时也是近一个半月,不过也学到了很多东西,也算是收获颇多吧(我这一个半月怎么会打了65小时LOL.....也没啥印象啊,看来游戏还是使人忘我)

Last modification:July 21st, 2020 at 10:48 pm