网上搜了一些app添加密码锁的方法,但是都太麻烦了。。。想来想去其实加密码的目的就是不让别人看到主界面的内容,那弄个界面遮起来不就行了,,所以打算弄一个对话框一直显示在主界面挡住内容,直到输对密码才消失(其实就是懒得折腾常规方法)

添加xml界面

大概需要二个界面,点击添加密码有一个对话框,选择添加数字密码,然后有一个输入密码的界面,遮挡内容的界面也用这个(懒)

password_add.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_marginTop="10dp"
           android:layout_marginBottom="20dp"
        android:id="@+id/password_num_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        android:foreground="?android:attr/selectableItemBackground"
        android:text="数字密码" />

</LinearLayout>

password_num_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_margin="50dp"
        android:id="@+id/password_num_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:maxLength="4"
        />

    <Button
        android:text="确定"
        android:background="@drawable/confirm_btn_round"
        android:id="@+id/password_num_confirm"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="50dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

对话框

选择密码类型对话框

private void Add_Password(){
        final  AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle("添加密码");
        View view=LayoutInflater.from(getContext()).inflate(R.layout.password_add,null);
        builder.setView(view);
        Button password_num_btn=view.findViewById(R.id.password_num_btn);
        password_num_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Add_Num_Dialog();
            }
        });
        builder.show();
    }

添加数字密码对话框

//储存数字密码
List<String> Number_passwords=new ArrayList<>();
//
private void Add_Num_Dialog(){
        final AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
        builder.setTitle("输入4位数字密码");
        final View view=LayoutInflater.from(getContext()).inflate(R.layout.password_num_dialog,null);
        builder.setView(view);
        final EditText num_edit=view.findViewById(R.id.password_num_edit);
        Button confirm_btn=view.findViewById(R.id.password_num_confirm);
        final AlertDialog dialog=builder.show();
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Get_Password(Number_passwords,"num_password");
                if (num_edit.getText().toString().length()==4){
                    if (Number_passwords.contains(num_edit.getText().toString())){
                        Toast.makeText(getActivity(), "您已经添加过该密码!", Toast.LENGTH_SHORT).show();
                    }else {
                        Number_passwords.add(num_edit.getText().toString());
                        Save_Password(Number_passwords,"num_password");
                        Toast.makeText(getActivity(), "已添加密码!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }
                else {
                    Toast.makeText(getActivity(), "请输入4位数密码!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//
//
    private void  Save_Password(List<String> arrayList, String s){
        SharedPreferences sp=getContext().getSharedPreferences(s,Context.MODE_PRIVATE);
        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();
    }
    private void Get_Password(List<String> arrayList,String s){
        SharedPreferences sp=getContext().getSharedPreferences(s,Context.MODE_PRIVATE);
        int size=sp.getInt("size",0);
        for(int i=0;i<size;i++) {
            arrayList.add (sp.getString(s+i,null));
        }
    }

传值

fragment与activity传值

在fragment中定义接口

private MyListener myListener;
public interface MyListener{
    public void change_mode(Boolean mode);
}
//
//
//回调
@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        myListener=(MyListener)getContext();
    }

//
//
findPreference("safe_mode").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                SAFE_MODE = (Boolean) newValue;
                myListener.change_mode(SAFE_MODE);
                return true;
            }
        });

activity中

private Boolean SAFE_MODE;
//
@Override
    public void change_mode(Boolean mode) {
        SAFE_MODE=mode;
    }

activity互相传值

MainActivity.java

SharedPreferences sp=getSharedPreferences("safe_mode",MODE_PRIVATE);
SAFE_MODE=sp.getBoolean("safe_mode",false);
//存储模式
//
Intent intent=new Intent(this,SettingsActivity.class);
startActivityForResult(intent,1);
//
//
@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 1:{
                if (resultCode==RESULT_OK){
                    assert data != null;
                    SAFE_MODE=data.getBooleanExtra("safe_mode",false);
                    SharedPreferences sp=getSharedPreferences("safe_mode",MODE_PRIVATE);
                    sp.edit().putBoolean("safe_mode",SAFE_MODE).apply();
                    Toast.makeText(this, SAFE_MODE.toString(), Toast.LENGTH_SHORT).show();
                }break;
            }
            default:
        }
    }

SettingsActivity.java

Intent intent=new Intent(SettingsActivity.this,MainActivity.class);
                intent.putExtra("safe_mode",SAFE_MODE);
                setResult(RESULT_OK,intent);
                finish();

其实可以直接用SharedPreferences“传值”的,但是想多试一些方法,不过后面可能会用到这个方法来匹配密码,日后再说!

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