以编程方式在EditText中设置密码点

时间:2013-11-07 08:58:08

标签: android

我一直在尝试以编程方式将edittext设置为密码字段,如下所示:

方法1:

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

方法2:

password.setTransformationMethod(PasswordTransformationMethod.getInstance());

方法3:

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

方法4:

    public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;
        public PasswordCharSequence(CharSequence source) {
            mSource = source; // Store char sequence
        }
        public char charAt(int index) {
            return '*'; // This is the important part
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};

// Call the above class using this:
text.setTransformationMethod(new MyPasswordTransformationMethod());

我创建了我的edittext,如下所示:

        // Create the password edittext
    EditText etPwrd = new EditText(this);

    // Customise the password edittext
    etPwrd.setLayoutParams(etPwrdParams);
    etPwrd.setBackgroundResource(R.drawable.etlogin);
    etPwrd.setTextSize(18f);
    etPwrd.setLongClickable(false);
    etPwrd.setPadding (5,0,0,0);
    etPwrd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    etPwrd.setTransformationMethod(new MyPasswordTransformationMethod());
    etPwrd.setTypeface(officialRegularFont);
    //etPwrd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    //etPwrd.setTransformationMethod(PasswordTransformationMethod.getInstance());
    etPwrd.setSingleLine();
    etPwrd.setHint(R.string.password_text);
    etPwrd.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

    });

我使用上述所有方法都失败了,因为当用户输入字符时,edittext仍会显示字符。

我在XML中知道如何将edittext设置为密码字段,但我需要以编程方式进行此操作。

2 个答案:

答案 0 :(得分:5)

您无需创建其他密码转换方法,您可以这样做。

password.setTransformationMethod(PasswordTransformationMethod.getInstance());

答案 1 :(得分:4)

在onCreate()中,在创建EditText时添加这两行

    et2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
    et2.setTransformationMethod(new MyPasswordTransformationMethod());

然后在同样的活动中创建MyPasswordTransformationMethod

public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;
        public PasswordCharSequence(CharSequence source) {
            mSource = source; // Store char sequence
        }
        public char charAt(int index) {
            return '*'; // This is the important part
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};