📜  如何在Android中生成图案密码?

📅  最后修改于: 2021-05-10 17:00:10             🧑  作者: Mango

该设备的图案密码是保持我们的设备私有和受保护的必要条件之一。如今,在大多数应用程序中,我们可以看到此密码已在许多应用程序(例如互惠基金或股票市场应用程序)中应用,以使我们的财务信息不公开。在本文中,我们将了解如何在我们的Android应用中实现模式密码。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。

在Android示例GIF中生成图案密码

图案密码的应用

  • 用于在Android应用中保护我们的私人详细信息和个人信息。
  • 使用模式密码而不是密码变得非常方便。
  • 它可作为应用程序锁定到我们设备上的应用程序。

模式密码的属性

Attributes Description
layout_width To display the width of the dots.
layout_height To display the height of the dots.
correctStateColor To display Dots color.

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:在build.gradle文件中添加模式密码库的依赖项

然后导航到gradle脚本,然后到build.gradle(Module)级别。在依赖性部分的build.gradle文件中添加以下行。

现在单击“立即同步” ,它将同步build.gradle()中的所有文件

步骤3:在您的activity_main.xml文件中创建一个加载屏幕

导航到应用程序> res>布局以打开activity_main.xml文件。以下是activity_main.xml文件的代码。

XML


  
    
    
      


Java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // loading is given
                SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
                String password = sharedPreferences.getString("password", "0");
                if (password.equals("0")) {
                    // Intent to navigate to Create Password Screen
                    Intent intent = new Intent(getApplicationContext(), CreatePasswordActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Intent to navigate to Input Password Screen
                    Intent intent = new Intent(getApplicationContext(), InputPasswordActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }, 2000);
    }
}


XML


  
    
    
  
    
    
  


Java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
  
import java.util.List;
  
public class CreatePasswordActivity extends AppCompatActivity {
  
    // Initialize pattern lock view
    PatternLockView mPatternLockView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_password);
  
        mPatternLockView = (PatternLockView) findViewById(R.id.pattern_lock_view);
        mPatternLockView.addPatternLockListener(new PatternLockViewListener() {
            @Override
            public void onStarted() {
  
            }
  
            @Override
            public void onProgress(List progressPattern) {
  
            }
  
            @Override
            public void onComplete(List pattern) {
                // Shared Preferences to save state
                SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("password", PatternLockUtils.patternToString(mPatternLockView, pattern));
                editor.apply();
                  
                // Intent to navigate to home screen when password added is true
                Intent intent = new Intent(getApplicationContext(), ProgramActivity.class);
                startActivity(intent);
                finish();
            }
            @Override
            public void onCleared() {
  
            }
        });
    }
}


XML


  
    
    
  
    
    
  


Java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
  
import java.util.List;
  
public class InputPasswordActivity extends AppCompatActivity {
  
    PatternLockView mPatternLockView;
    String password;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_password);
          
        // shared preference when user comes second time to the app
        SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
        password = sharedPreferences.getString("password", "0");
          
        mPatternLockView = (PatternLockView) findViewById(R.id.pattern_lock_view);
        mPatternLockView.addPatternLockListener(new PatternLockViewListener() {
            @Override
            public void onStarted() {
  
            }
  
            @Override
            public void onProgress(List progressPattern) {
  
            }
  
            @Override
            public void onComplete(List pattern) {
                // if drawn pattern is equal to created pattern you will navigate to home screen
                if (password.equals(PatternLockUtils.patternToString(mPatternLockView, pattern))) {
                    Intent intent = new Intent(getApplicationContext(), ProgramActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // other wise you will get error wrong password
                    Toast.makeText(InputPasswordActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
                    mPatternLockView.clearPattern();
                }
            }
  
            @Override
            public void onCleared() {
  
            }
        });
    }
}


XML


  
    
    
      


步骤4:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // loading is given
                SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
                String password = sharedPreferences.getString("password", "0");
                if (password.equals("0")) {
                    // Intent to navigate to Create Password Screen
                    Intent intent = new Intent(getApplicationContext(), CreatePasswordActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Intent to navigate to Input Password Screen
                    Intent intent = new Intent(getApplicationContext(), InputPasswordActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }, 2000);
    }
}

步骤5:创建一个新的空Activity并将其命名为CreatePasswordActivity

转到应用程序> Java >您的应用程序的程序包名称>右键单击>新建>活动>空活动,然后将该活动命名为CreatePasswordActivity

使用activity_create_password.xml文件:

转到activity_create_password.xml文件,并参考以下代码。以下是activity_create_password.xml文件的代码在此文件中,将显示如下所示的点以创建图案密码。

XML格式



  
    
    
  
    
    
  

使用CreatePasswordActivity。 Java文件:

转到CreatePasswordActivity。 Java文件并参考以下代码。下面是CreatePasswordActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
  
import java.util.List;
  
public class CreatePasswordActivity extends AppCompatActivity {
  
    // Initialize pattern lock view
    PatternLockView mPatternLockView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_password);
  
        mPatternLockView = (PatternLockView) findViewById(R.id.pattern_lock_view);
        mPatternLockView.addPatternLockListener(new PatternLockViewListener() {
            @Override
            public void onStarted() {
  
            }
  
            @Override
            public void onProgress(List progressPattern) {
  
            }
  
            @Override
            public void onComplete(List pattern) {
                // Shared Preferences to save state
                SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("password", PatternLockUtils.patternToString(mPatternLockView, pattern));
                editor.apply();
                  
                // Intent to navigate to home screen when password added is true
                Intent intent = new Intent(getApplicationContext(), ProgramActivity.class);
                startActivity(intent);
                finish();
            }
            @Override
            public void onCleared() {
  
            }
        });
    }
}

步骤6:类似地创建一个新的空Activity,并将其命名为InputPasswordActivity

转到应用程序> Java >您的应用程序的程序包名称>右键单击>新建>活动>空活动,然后将该活动命名为InputPasswordActivity

使用activity_Input_Password.xml文件:

转到activity_Input_Password.xml文件,并参考以下代码。以下是activity_Input_Password.xml文件的代码在这个 文件, 安装后,当用户第二次打开应用程序时,您将绘制图案以导航到主屏幕。

XML格式



  
    
    
  
    
    
  

使用InputPasswordActivity。 Java文件:

转到InputPasswordActivity。 Java文件并参考以下代码。下面是InputPasswordActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
  
import java.util.List;
  
public class InputPasswordActivity extends AppCompatActivity {
  
    PatternLockView mPatternLockView;
    String password;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_password);
          
        // shared preference when user comes second time to the app
        SharedPreferences sharedPreferences = getSharedPreferences("PREFS", 0);
        password = sharedPreferences.getString("password", "0");
          
        mPatternLockView = (PatternLockView) findViewById(R.id.pattern_lock_view);
        mPatternLockView.addPatternLockListener(new PatternLockViewListener() {
            @Override
            public void onStarted() {
  
            }
  
            @Override
            public void onProgress(List progressPattern) {
  
            }
  
            @Override
            public void onComplete(List pattern) {
                // if drawn pattern is equal to created pattern you will navigate to home screen
                if (password.equals(PatternLockUtils.patternToString(mPatternLockView, pattern))) {
                    Intent intent = new Intent(getApplicationContext(), ProgramActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // other wise you will get error wrong password
                    Toast.makeText(InputPasswordActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
                    mPatternLockView.clearPattern();
                }
            }
  
            @Override
            public void onCleared() {
  
            }
        });
    }
}

步骤7:同样,创建一个新的空Activity并将其命名为ProgramActivity

转到应用程序> Java >您的应用程序的程序包名称>右键单击>新建>活动>空活动,然后将该活动命名为ProgramActivity

使用activity_program.xml文件:

转到activity_program.xml文件,并参考以下代码。以下是activity_program.xml文件的代码在此文件中,我们将创建主屏幕,以用户导航主屏幕。您将在哪里看到短信。

XML格式



  
    
    
      

现在单击运行选项,将需要一些时间来构建Gradle。之后,您将在设备上获得输出,如下所示。

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!