实现密码输入

效果如图

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();
    }

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

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

原文地址:https://www.cnblogs.com/rainhome/p/5339455.html