Android5.0 PIN码输入框限制输入个数确定按键置灰

来源:互联网 发布:长虹网络电视看直播 编辑:程序博客网 时间:2024/05/16 11:54
需求:在输入pin码对话框界面,如果输入少于4位数,确定键置灰失去焦点

思路:获取确定键引用,监听输入个数,当个数少于4位使确定键置灰

注意:确定键的引用只有在dialog  show出来之后才可以引用

EditPinPreference.java中添加  监听输入的个数private int mMinLenght = 0;    public void setMinLength(int len){        mMinLenght = len;    }    @Override    protected void onDialogCreated(){        super.onDialogCreated();        editPin();    }    public void editPin(){        final EditText editText = getEditText();        editText.addTextChangedListener(mTextWatcher);        final Dialog dialog = getDialog();        if (dialog == null) return;        final Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);        positiveButton.setEnabled(false);    }    private TextWatcher mTextWatcher = new TextWatcher() {        @Override        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {            //Log.d(TAG, "beforeTextChanged...");        }        @Override        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {            final EditText editText = getEditText();            final Dialog dialog = getDialog();            if (dialog == null) return;            final Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);//            Log.d(TAG, "onTextChanged..." + dialog);            if (editText != null && charSequence.length() < mMinLenght) {                positiveButton.setEnabled(false);            } else {                positiveButton.setEnabled(true);            }        }        @Override        public void afterTextChanged(Editable editable) {//                Log.d(TAG, "afterTextChanged...");        }    };*********在frameworks/base/core/java/android/prefrence/EditTextPreference.java  中增加 /**     *  * @hide     */    protected void onDialogCreated(){        super.onDialogCreated();    }*********在frameworks/base/core/java/android.preference/DialogPreference.java中添加 /**     ** @hide     */    protected void onDialogCreated(){    }然后在dialog.show();后边添加   dialog.setOnDismissListener(this);        dialog.show();        onDialogCreated();*****************当dialog  show出来之后直接执行此方法,进行监听*********在Settings/src/com/android/Setting/IccLockSetting.java中在mPinDialog获取之后,再设置输入最小长度(确定键置灰)        mPinDialog = (EditPinPreference) findPreference(PIN_DIALOG);        mPinToggle = (SwitchPreference) findPreference(PIN_TOGGLE);        mPinDialog.setMinLength(4);


1 0