当前位置:首页 » 《资源分享》 » 正文

Android 四位数字密码验证器类(带界面)_Remarkable_的博客

16 人参与  2021年10月21日 14:23  分类 : 《资源分享》  评论

点击全文阅读


第一次发文章,如有错误请多包涵,感谢指正。

请勿用于商业用途。

使用方法:

//调用
    String RIGHT_PASSWORD = "1234";
    Intent intentSet = new Intent(MainActivity.this, password_input.class);
    Bundle bundle = new Bundle();
    bundle.putString("right_password", RIGHT_PASSWORD);
    intentSet.putExtras(bundle);
    startActivityForResult(intentSet, 1);

//处理是否通过密码验证
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        password_correct = data.getExtras().getBoolean("password_correct");//得到新Activity 关闭后返回的数据
        if(password_correct){
            //密码输入正确
        }else{
            //密码输入错误
        }
    }

I.password_input.java

package com.example.safesystem;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class password_input extends AppCompatActivity {
    String input_password = "";
    String right_password = "";
    boolean password_correct=false;
    int tmp = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_input);

        hideBottomUIMenu();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        passwordNumboard();

    }

    public void passwordNumboard(){
        input_password = "";
        right_password = getIntent().getStringExtra("right_password");
        //TODO: 做密码验证器

        List<TextView> numButton = new ArrayList<TextView>();
        numButton.add((TextView) findViewById(R.id.button_num0));
        numButton.add((TextView) findViewById(R.id.button_num1));
        numButton.add((TextView) findViewById(R.id.button_num2));
        numButton.add((TextView) findViewById(R.id.button_num3));
        numButton.add((TextView) findViewById(R.id.button_num4));
        numButton.add((TextView) findViewById(R.id.button_num5));
        numButton.add((TextView) findViewById(R.id.button_num6));
        numButton.add((TextView) findViewById(R.id.button_num7));
        numButton.add((TextView) findViewById(R.id.button_num8));
        numButton.add((TextView) findViewById(R.id.button_num9));


        numButton.get(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=0;
                displayUpdate();
            }
        });
        numButton.get(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=1;
                displayUpdate();
            }
        });
        numButton.get(2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=2;
                displayUpdate();
            }
        });
        numButton.get(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=3;
                displayUpdate();
            }
        });
        numButton.get(4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=4;
                displayUpdate();
            }
        });
        numButton.get(5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=5;
                displayUpdate();
            }
        });
        numButton.get(6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=6;
                displayUpdate();
            }
        });
        numButton.get(7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=7;
                displayUpdate();
            }
        });
        numButton.get(8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=8;
                displayUpdate();
            }
        });
        numButton.get(9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_password+=9;
                displayUpdate();
            }
        });


        ((ImageView)findViewById(R.id.button_back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(input_password.length()>=1){
                    input_password = input_password.substring(0,input_password.length()-1);
                }
                displayUpdate();
            }
        });

        ((ImageView)findViewById(R.id.button_ok)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password_correct = input_password.equals(right_password);
                Intent intent = new Intent();
                Bundle bundleRet = new Bundle();
                bundleRet.putBoolean("password_correct",password_correct);
                //把返回数据存入Intent
                intent.putExtras(bundleRet);
                //设置返回数据
                password_input.this.setResult(RESULT_OK, intent);
                //关闭Activity
                password_input.this.finish();
            }
        });
    }

    void displayUpdate(){
        switch (input_password.length()){
            case 0:
                ((ImageView)findViewById(R.id.password_1)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_2)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_3)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_4)).setImageResource(R.drawable.pic_password_notclicked);
                break;
            case 1:
                ((ImageView)findViewById(R.id.password_1)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_2)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_3)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_4)).setImageResource(R.drawable.pic_password_notclicked);
                break;
            case 2:
                ((ImageView)findViewById(R.id.password_1)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_2)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_3)).setImageResource(R.drawable.pic_password_notclicked);
                ((ImageView)findViewById(R.id.password_4)).setImageResource(R.drawable.pic_password_notclicked);
                break;
            case 3:
                ((ImageView)findViewById(R.id.password_1)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_2)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_3)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_4)).setImageResource(R.drawable.pic_password_notclicked);
                break;
            case 4:
                ((ImageView)findViewById(R.id.password_1)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_2)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_3)).setImageResource(R.drawable.pic_password_clicked);
                ((ImageView)findViewById(R.id.password_4)).setImageResource(R.drawable.pic_password_clicked);
                break;
        }
        if(input_password.length()==4){
            password_correct = input_password.equals(right_password);
            Intent intent = new Intent();
            Bundle bundleRet = new Bundle();
            bundleRet.putBoolean("password_correct",password_correct);
            //把返回数据存入Intent
            intent.putExtras(bundleRet);
            //设置返回数据
            password_input.this.setResult(RESULT_OK, intent);
            //关闭Activity
            password_input.this.finish();
        }
    }


    protected void hideBottomUIMenu() {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

}

II.activity_password_input.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".password_input">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        app:layout_constraintVertical_weight="1"
        app:layout_constraintHorizontal_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/password_space_left"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <ImageView
                    android:id="@+id/password_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:src="@drawable/pic_password_notclicked" />

                <ImageView
                    android:id="@+id/password_2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:src="@drawable/pic_password_notclicked" />

                <ImageView
                    android:id="@+id/password_3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:src="@drawable/pic_password_notclicked" />

                <ImageView
                    android:id="@+id/password_4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:src="@drawable/pic_password_notclicked" />

                <ImageView
                    android:id="@+id/password_space_right"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/button_num1"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="1"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num2"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="2"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num3"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="3"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <ImageView
                    android:id="@+id/button_back"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:src="@drawable/pic_password_back"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/button_num4"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="4"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num5"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="5"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num6"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="6"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num0"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="0"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/button_num7"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="7"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num8"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="8"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <Button
                    android:id="@+id/button_num9"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:text="9"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />

                <ImageView
                    android:id="@+id/button_ok"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"

                    android:src="@drawable/pic_password_ok"
                    android:textColor="#FFFFFF"
                    android:textSize="25pt" />
            </LinearLayout>

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3" />
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>


点击全文阅读


本文链接:http://zhangshiyu.com/post/30075.html

返回  数据  密码  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1