实现密码输入

效果如图

1 首先这是一个自定义的Dialog,而不是AlertDialog,如果是AlertDialog的话,软键盘弹出的时候在AlertDialog的后面,无法进行输入。

2 Dialog的上面会有一个黑框,添加Style

 <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- 边框 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 无标题 -->
        <item name="android:windowBackground">@color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 模糊 -->
    </style>

3 输入密码用的是EditText,改变EditText的格式是

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners android:radius="3dip"/>
    <stroke
        android:width="1dip"
        android:color="@color/viewfinder_mask" />
</shape>

4 给EditText添加监听函数,本来想把EditText放入到List来管理,结果总是报错指针错误,只能作罢- -,所以看起来有很多重复的代码啊,要监听EditText的内容改变,限制输入的内容,默认弹出数字键盘,监听键盘的删除键,以下是实现代码

void initDialog() {
         current=0;
         myDialog = new Dialog(ChargeActivity.this, R.style.add_dialog);
        myDialog.show();
        myDialog.getWindow().setContentView(R.layout.return_code_dialog);
        psw1 = (EditText) myDialog.getWindow().findViewById(R.id.psw_1);
        psw2 = (EditText) myDialog.getWindow().findViewById(R.id.psw_2);
        psw3 = (EditText) myDialog.getWindow().findViewById(R.id.psw_3);
        psw4 = (EditText) myDialog.getWindow().findViewById(R.id.psw_4);
        psw5 = (EditText) myDialog.getWindow().findViewById(R.id.psw_5);
        psw6 = (EditText) myDialog.getWindow().findViewById(R.id.psw_6);

        //锁定数字键盘
        psw1.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw2.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw3.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw4.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw5.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw6.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

        psw1.addTextChangedListener(this);
        psw2.addTextChangedListener(this);
        psw3.addTextChangedListener(this);
        psw4.addTextChangedListener(this);
        psw5.addTextChangedListener(this);
        psw6.addTextChangedListener(this);

         psw1.setOnKeyListener(this);
         psw2.setOnKeyListener(this);
         psw3.setOnKeyListener(this);
         psw4.setOnKeyListener(this);
         psw5.setOnKeyListener(this);
         psw6.setOnKeyListener(this);

    }

        @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length =0;
        //Log.d("test","current="+current+"我变了");
        switch(current){
            case 0:length=psw1.getText().length();changeFocus(length,psw2);passwordInput[0]=psw1.getText().toString();break;
            case 1:length=psw2.getText().length();changeFocus(length,psw3);passwordInput[1]=psw2.getText().toString();break;
            case 2:length=psw3.getText().length();changeFocus(length,psw4);passwordInput[2]=psw3.getText().toString();break;
            case 3:length=psw4.getText().length();changeFocus(length,psw5);passwordInput[3]=psw4.getText().toString();break;
            case 4:length=psw5.getText().length();changeFocus(length,psw6);passwordInput[4]=psw5.getText().toString();break;
            case 5:length=psw6.getText().length();if(length>=1){passwordInput[5]=psw6.getText().toString();Log.d("input",passwordInput[5]);validatePassword();myDialog.cancel();}
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    void changeFocus(int l,EditText editText){
        if(l >= maxLen){
            current++;
            //Log.d("test", "current=" + current+" 请求焦点");
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.requestFocus();
            editText.findFocus();
            /**if(editText.isFocusable()){
                Log.d("test",editText.getId()+"获得焦点");
            }*/
        }
    }
    void validatePassword(){
        current=0;
        boolean isInSchool =true;
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<passwordInput.length;i++){
            stringBuffer.append(passwordInput[i]);
        }
        String psw =stringBuffer.toString();
        boolean isPassword =psw.equals(password);
        Log.d("psw",passwordInput.toString());
        Log.d("psw",password);
        if(!isPassword){
            final Dialog errorDialog =new AlertDialog.Builder(ChargeActivity.this).create();
            errorDialog.show();
            errorDialog.getWindow().setContentView(R.layout.error_password_dialog);
            errorDialog.getWindow().findViewById(R.id.ok_error).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    errorDialog.cancel();
                    initDialog();
                }
            });
        }else if(!isInSchool){
            final Dialog out =new AlertDialog.Builder(ChargeActivity.this).create();
            out.show();
            out.getWindow().setContentView(R.layout.out_school_dialog);
            out.getWindow().findViewById(R.id.ok_out_school).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    out.cancel();
                }
            });
        }else{
            startActivity(new Intent(ChargeActivity.this,PaymentActivity.class));
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            current--;
            switch(current){
                case 0:psw1.setText("");requestF(psw1);break;
                case 1:psw2.setText("");requestF(psw2);break;
                case 2:psw3.setText("");requestF(psw3);break;
                case 3:psw4.setText("");requestF(psw4);break;
                case 4:psw5.setText("");requestF(psw5);break;
                case 5:psw6.setText("");requestF(psw6);break;
            }
        }
        return false;
    }
    void requestF(EditText editText){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.findFocus();
    }

写的过程中犯了一个比较傻的错误,字符串数组没分配内存,结果一直报指针错误,傻呵呵的调了半天= =

还有就是一些逻辑细节的问题,向删除的时候比较容易引起焦点混乱。

时间: 2024-08-28 02:36:03

实现密码输入的相关文章

Demon_游戏登录界面(具备账号密码输入功能)

using UnityEngine; using System.Collections; using UnityEngine.UI;// public class LoginButton : MonoBehaviour { //正确的用户名密码 public string uname = "wasd"; public string pwd = "123"; //用户名输入框 public InputField unameInput; //密码输入框 public I

EditTextPreference点击后输入框显示隐藏内容,类似密码输入(转)

http://bbs.anzhuo.cn/thread-928131-1-1.html EditTextPreference点击后输入框显示隐藏内容,类似密码输入... [复制链接]     askilledhand ADD.幼儿园 UID 2186431 帖子 46 精华 0 积分 34 最后登录 2014-3-5 串个门 加好友 打招呼 发消息 电梯直达 1楼  发表于 2013-11-18 11:59:03 |只看该作者 |倒序浏览  一键分享 [新人报到]现在去发帖报道即可领取论坛金币哦

WebBrowser无法显示招商银行密码输入控件的问题

本文由CharlesSimonyi发表于CSDN博客:http://blog.csdn.net/charlessimonyi/article/details/30479131转载请注明出处 之前就看到CSDN论坛上有人提问,自己写的程序中的WebBrowser打开招商银行的登录页面后(https://pbnj.ebank.cmbchina.com/CmbBank_GenShell/UI/GenShellPC/Login/Login.aspx),无法显示密码输入控件,但是在IE中可以正常显示. 后

1、账号密码输入次数 &nbsp; 2、多级菜单

1.账号密码输入次数 条件:1.输入用户密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 user="pengchun" password="pcwangjixuan" f = open('user.log','r') lock_file = f.read() f.close() count = 0 for i in range(3): user1=input("please input your name:") password1 = in

实现免密码输入 ssh 登录

实现免密码输入 ssh 登录假设 A 为客户机器, B 为目标机:要达到的目的:A 机器 ssh 登录 B 机器无需输入密码:加密方式选 rsa|dsa 均可以,默认 dsa做法:1.登录 A 机器2. ssh-keygen -t [rsa|dsa],将会生成密钥文件和私钥文件 id_rsa, id_rsa.pub 或 id_dsa,id_dsa.pub3.将 .pub 文件复制到 B 机器的 .ssh 目录, 并 cat id_dsa.pub >> ~/.ssh/authorized_key

Java学习:一个仿ATM机键盘做的密码输入

1.程序运行截图: 2.程序实现的功能 1)进入程序时,程序会询问你输入一个密码 2)进入键盘界面后,按下数字0-9可以输入一个字符,按取消按钮可以退出程序,重置按钮可以将上面的JPasswordField变为空,按下确定按钮,提示输入的密码是否正确 3.程序代码 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays;

shell 密码输入不显示,Shell输出内容不显示密码,Shell实现有密码自动登录sshpass 应用实践

在很多实践项目中,我们经常会通过SSH来进行认证,如通过SSH拷贝文件,虽然我们可以使用公钥与私钥实现ssh 无密码登录,在不同的服务器上又需要配对相应的密钥,切换用户麻烦等问题,在一些需要交互但会涉及到批量处理的时候,通过shell 密码输入不显示,Shell输出内容不显示密码,Shell实现有密码自动登录会大大的提高工作效率 #! /bin/bash ############################################## #Author:                

模拟三次密码输入

#include <stdio.h> #include<string.h> int main() {  int i=0;  char *p = "123456";  char passwd[10];  for (i = 0; i < 3;i++)  {   printf("请输入密码(最多9位):>");   scanf("%s",passwd);   if (strcmp(p,passwd) == 0)   

模拟银行用户三次密码输入

#include<stdio.h> #include <string.h> int main() { int i,j; char arr1[10]="123456"; char arr2[10]; printf("请输入密码:\n"); for(i=0;i<3;i++) { scanf("%s",&arr2); if(strcmp(arr1,arr2)==0) { break; } else { printf

密码输入强度提示实例代码

密码输入强度提示实例代码:现在众多的网站的注册表单,在填写密码的时候能够实时的给出密码强度提示,这可以提醒用户当前输入的密码安全程度,算是非常人性化的一个举措,下面就通过一个实例简单介绍一下如何实现此效果.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ww