下面列出了怎么用android.text.method.PasswordTransformationMethod的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 设置密码文本视图显示转换
* @param editText {@link EditText}
* @param isDisplayPassword 是否显示密码
* @param isSelectBottom 是否设置光标到最后
* @param <T> 泛型
* @return {@link EditText}
*/
public static <T extends EditText> T setTransformationMethod(final T editText, final boolean isDisplayPassword, final boolean isSelectBottom) {
if (editText != null) {
// 获取光标位置
int curSelect = 0;
if (!isSelectBottom) {
curSelect = getSelectionStart(editText);
}
editText.setTransformationMethod(isDisplayPassword ?
HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
if (isSelectBottom) { // 设置光标到最后
setSelectionToBottom(editText);
} else { // 设置光标到之前的位置
setSelection(editText, curSelect);
}
}
return editText;
}
private void setInputType() {
if (inputType != null) {
switch (inputType) {
case "textPassword": {
editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
togglePassword.setVisibility(View.VISIBLE);
editText.setPadding(
Utils.dp2px(context, 15),
Utils.dp2px(context, 5),
Utils.dp2px(context, 50),
Utils.dp2px(context, 5)
);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
break;
}
case "number": {
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
break;
}
}
}
}
private void updateDropDownTextView() {
if (dropDown != null) {
if (currentPasswordType == 0) {
dropDown.setText(LocaleController.getString("PasscodePIN", R.string.PasscodePIN));
} else if (currentPasswordType == 1) {
dropDown.setText(LocaleController.getString("PasscodePassword", R.string.PasscodePassword));
}
}
if (type == 1 && currentPasswordType == 0 || type == 2 && SharedConfig.passcodeType == 0) {
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(4);
passwordEditText.setFilters(filterArray);
passwordEditText.setInputType(InputType.TYPE_CLASS_PHONE);
passwordEditText.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
} else if (type == 1 && currentPasswordType == 1 || type == 2 && SharedConfig.passcodeType == 1) {
passwordEditText.setFilters(new InputFilter[0]);
passwordEditText.setKeyListener(null);
passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
private void beautyView() {
beautyEditText(this.mInputAccount, L10NString.getString("umgr_please_input_username"), this.mAccountTextWatcher);
beautyCleanButton(this.mClearInputAccount, this);
this.mInputAccountLayout.setOnClickListener(this);
beautyCleanButton(this.mClearInputPassword, this);
this.mInputPassword.setOnClickListener(this);
beautyEditText(this.mInputPassword, L10NString.getString("umgr_please_input_password"), this.mPasswordTextWatcher);
beautyColorTextView(this.mRegister, "#007dc4", false, L10NString.getString("umgr_whether_register_ornot"), this);
beautyColorTextView(this.mFindpwd, "#007dc4", false, L10NString.getString("umgr_whether_forget_password"), this);
beautyColorTextView(this.mSwitchAccount, "#007dc4", false, L10NString.getString("umgr_third_login_qihoo_tip"), this);
beautyButtonGreen(this.mLogin, L10NString.getString("umgr_login"), this);
beautyTextView(this.mAgreeClause1, L10NString.getString("umgr_login_agree_clause_1"));
beautyTextView(this.mAgreeClauseUser, L10NString.getString("umgr_agree_clause_2_user"));
beautyColorTextView(this.mAgreement, "#0099e5", true, L10NString.getString("umgr_agree_clause_2_agreement"), this);
beautyTextView(this.mAnd, L10NString.getString("umgr_agree_clause_2_and"));
beautyColorTextView(this.mPrivacy, "#0099e5", true, L10NString.getString("umgr_agree_clause_2_privacy"), this);
beautyCheckButton(this.mShowPwd, new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
LoginActivity.this.mInputPassword.setTransformationMethod(LoginActivity.this.mShowPwd.isChecked() ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
}
});
loadPrivateConfig();
}
public void passwordVisibilityToggleRequested() throws NoSuchFieldException, IllegalAccessException {
// Store the current cursor position
int selection = getEditText().getSelectionEnd();
if (!getEditText().getText().toString().isEmpty()) {
getEditText().setTransformationMethod(PasswordTransformationMethod.getInstance());
toggleEnabled("mPasswordToggledVisible", false);
mPasswordToggleView.setChecked(false);
} else {
getEditText().setTransformationMethod(null);
toggleEnabled("mPasswordToggledVisible", true);
mPasswordToggleView.setChecked(true);
}
// And restore the cursor position
getEditText().setSelection(selection);
}
public void passwordVisibilityToggleRequested() throws NoSuchFieldException, IllegalAccessException {
// Store the current cursor position
int selection = getEditText().getSelectionEnd();
if (!getEditText().getText().toString().isEmpty()) {
getEditText().setTransformationMethod(PasswordTransformationMethod.getInstance());
toggleEnabled("mPasswordToggledVisible", false);
mPasswordToggleView.setChecked(false);
} else {
getEditText().setTransformationMethod(null);
toggleEnabled("mPasswordToggledVisible", true);
mPasswordToggleView.setChecked(true);
}
// And restore the cursor position
getEditText().setSelection(selection);
}
/**
* Creates a new PasswordTextBox component.
*
* @param container container, component will be placed in
*/
public PasswordTextBox(ComponentContainer container) {
super(container, new EditText(container.$context()));
// make the box single line
view.setSingleLine(true);
// Add a transformation method to hide password text. This must
// be done after the SingleLine command
view.setTransformationMethod(new PasswordTransformationMethod());
// make sure the done action is Done and not Next. See comment in Textbox.java
view.setImeOptions(EditorInfo.IME_ACTION_DONE);
PasswordVisible(false);
}
@Test
public void testSetPasswordToggleProgrammatically() {
// Set edit text input type to be password
onView(withId(R.id.textinput_no_icon))
.perform(setTransformationMethod(PasswordTransformationMethod.getInstance()));
// Set end icon as the password toggle
onView(withId(R.id.textinput_no_icon))
.perform(setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE));
final Activity activity = activityTestRule.getActivity();
final TextInputLayout textInputLayout =
activity.findViewById(R.id.textinput_no_icon);
// Assert the end icon is the password toggle icon
assertEquals(TextInputLayout.END_ICON_PASSWORD_TOGGLE, textInputLayout.getEndIconMode());
}
public static $ pwd(boolean option){
try{
if(mView instanceof TextView){
TextView textView = (TextView) mView;
if(option)
textView.setTransformationMethod(new PasswordTransformationMethod());
else
textView.setTransformationMethod(null);
}
else if(mView instanceof EditText){
EditText editText = (EditText) mView;
if(option)
editText.setTransformationMethod(new PasswordTransformationMethod());
else
editText.setTransformationMethod(null);
}
}catch (Exception e){
Log.d(TAG, e.getMessage());
}
return $.getInstance();
}
/**
* 初始化视图
*/
private void initViews() {
accountEdit = (CleanEditText) this.findViewById(R.id.et_email_phone);
accountEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
accountEdit.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
passwordEdit = (CleanEditText) this.findViewById(R.id.et_password);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_GO);
passwordEdit.setTransformationMethod(PasswordTransformationMethod
.getInstance());
passwordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_GO) {
clickLogin();
}
return false;
}
});
}
/**
* 初始化视图
*/
private void initViews() {
accountEdit = (CleanEditText) this.findViewById(R.id.et_email_phone);
accountEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
accountEdit.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
passwordEdit = (CleanEditText) this.findViewById(R.id.et_password);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
passwordEdit.setImeOptions(EditorInfo.IME_ACTION_GO);
passwordEdit.setTransformationMethod(PasswordTransformationMethod
.getInstance());
passwordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_GO) {
clickLogin();
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
boolean touchable = ( getWidth() - mWidth - Interval < event.getX() ) && (event.getX() < getWidth() - Interval);
if (touchable) {
isVisible = !isVisible;
if (isVisible){
//设置EditText文本为可见的
setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//设置EditText文本为隐藏的
setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
}
return super.onTouchEvent(event);
}
void changeDisplay(View view, int pos) {
if(mShowPwd) {
return;
}
Account.Entry entry = mItems.get(pos);
if(entry.mType == AccountManager.EntryType.PASSWORD ||
entry.mType == AccountManager.EntryType.PIN) {
boolean showed = mPwdShowed.get(pos);
ViewHolder holder = (ViewHolder)view.getTag();
if(showed) {
holder.mValue.setTransformationMethod(
PasswordTransformationMethod.getInstance());
} else {
holder.mValue.setTransformationMethod(
SingleLineTransformationMethod.getInstance());
}
mPwdShowed.set(pos, !showed);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.ap_button:
if (isChecked) {
//Turn on AP
enableAP();
} else {
//Turn off AP
disableAP();
}
break;
case R.id.checkBox:
if (!isChecked) {
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
}
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.registration );
userDate = Calendar.getInstance();
userDate.set( 1970, Calendar.DECEMBER, 31 );
nameField = (EditText) findViewById( R.id.nameField );
passwordField = (EditText) findViewById( R.id.passwordField );
passwordField.setTypeface( Typeface.DEFAULT );
passwordField.setTransformationMethod( new PasswordTransformationMethod() );
verifyPasswordField = (EditText) findViewById( R.id.verifyPasswordField );
verifyPasswordField.setTypeface( Typeface.DEFAULT );
verifyPasswordField.setTransformationMethod( new PasswordTransformationMethod() );
emailField = (EditText) findViewById( R.id.emailField );
birthdateField = (EditText) findViewById( R.id.birthdateField );
birthdateField.setFocusable( false );
birthdateField.setOnClickListener( birthdateFieldListener );
genderRadio = (RadioGroup) findViewById( R.id.genderGroup );
findViewById( R.id.cancelButton ).setOnClickListener( cancelListener );
Button registerButton = (Button) findViewById( R.id.registerButton );
registerButton.setOnClickListener( registerListener );
}
/**
* Takes care if checked/unchecked box for showing/not showing password
*/
private void show_password() {
if (mChbShowPwd.isChecked()) {
mTextboxPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
return;
}
mTextboxPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
private void updateDropDownTextView()
{
if (dropDown != null)
{
if (currentPasswordType == 0)
{
dropDown.setText(LocaleController.getString("PasscodePIN", R.string.PasscodePIN));
}
else if (currentPasswordType == 1)
{
dropDown.setText(LocaleController.getString("PasscodePassword", R.string.PasscodePassword));
}
}
if (type == 1 && currentPasswordType == 0 || type == 2 && SharedConfig.passcodeType == 0)
{
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(4);
passwordEditText.setFilters(filterArray);
passwordEditText.setInputType(InputType.TYPE_CLASS_PHONE);
passwordEditText.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
}
else if (type == 1 && currentPasswordType == 1 || type == 2 && SharedConfig.passcodeType == 1)
{
passwordEditText.setFilters(new InputFilter[0]);
passwordEditText.setKeyListener(null);
passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_send_code:
sendCode();
break;
case R.id.tv_next_step:
nextStep();
break;
case R.id.iv_back:
onBackPressed();
break;
case R.id.iv_pwd_state:
isHidePwd = !isHidePwd;
int startNew = etPwdNew.getSelectionStart();
int startConfirm = etPwdConfirm.getSelectionStart();
if (isHidePwd) {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_show));
etPwdNew.setTransformationMethod(PasswordTransformationMethod.getInstance());
etPwdConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_hide));
etPwdNew.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
etPwdConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
etPwdNew.setSelection(startNew);
etPwdConfirm.setSelection(startConfirm);
break;
case R.id.tv_confirm:
resetPwd();
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finishPage(false);
break;
case R.id.iv_refresh:
loadCode();
break;
case R.id.iv_pwd_state:
isHidePwd = !isHidePwd;
int start = etPwd.getSelectionStart();
if (isHidePwd) {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_show));
etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_hide));
etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
etPwd.setSelection(start);
break;
case R.id.tv_login:
login();
break;
case R.id.tv_register:
RegisterActivity.startActivity(this, getEmailInfo());
break;
case R.id.tv_forget_pwd:
ForgetActivity.startActivity(this, getEmailInfo());
break;
}
}
private void setPwdState(ImageView imageView,
EditText editText,
boolean isHidePwd) {
int start = editText.getSelectionStart();
if (isHidePwd) {
imageView.setImageDrawable(hidePwdDrawable);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
imageView.setImageDrawable(showPwdDrawable);
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
editText.setSelection(start);
}
/**
* 设置是否要显示密码
*
* @param isHidePwd true:隐藏密码
* false: 显示密码
*/
private void setPwdState(boolean isHidePwd) {
int start = etPwd.getSelectionStart();
if (isHidePwd) {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_show));
etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
ivPwdState.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_pwd_hide));
etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
etPwd.setSelection(start);
}
/**
* 设置密码文本视图显示转换
* @param textView {@link TextView}
* @param isDisplayPassword 是否显示密码
* @param <T> 泛型
* @return {@link TextView}
*/
public static <T extends TextView> T setTransformationMethod(final T textView, final boolean isDisplayPassword) {
if (textView != null) {
textView.setTransformationMethod(isDisplayPassword ?
HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
}
return textView;
}
/**
* 隐藏密码
*/
private void hidePassword() {
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
et_password.postInvalidate();
// et_password.setInputType(InputType.TYPE_CLASS_TEXT
// | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
private void setViews()
{
label.setText(labelResId);
if (labelResId != R.string.empty) label.setVisibility(View.VISIBLE);
togglePassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
editText.setSelection(editText.getText().length());
});
}
private void setInputType() {
if (inputType != null) {
switch (inputType) {
case "textPassword": {
editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
togglePassword.setVisibility(View.VISIBLE);
editText.setPadding(
Utils.dp2px(context, 15),
Utils.dp2px(context, 5),
Utils.dp2px(context, 50),
Utils.dp2px(context, 5)
);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
break;
}
case "number": {
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
break;
}
case "textNoSuggestions":{
editText.setInputType(editText.getInputType() |
EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE |
EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
break;
}
}
}
editText.setTypeface(Typeface.DEFAULT);
}
private void updateDropDownTextView()
{
if (dropDown != null)
{
if (currentPasswordType == 0)
{
dropDown.setText(LocaleController.getString("PasscodePIN", R.string.PasscodePIN));
}
else if (currentPasswordType == 1)
{
dropDown.setText(LocaleController.getString("PasscodePassword", R.string.PasscodePassword));
}
}
if (type == 1 && currentPasswordType == 0 || type == 2 && SharedConfig.passcodeType == 0)
{
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(4);
passwordEditText.setFilters(filterArray);
passwordEditText.setInputType(InputType.TYPE_CLASS_PHONE);
passwordEditText.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
}
else if (type == 1 && currentPasswordType == 1 || type == 2 && SharedConfig.passcodeType == 1)
{
passwordEditText.setFilters(new InputFilter[0]);
passwordEditText.setKeyListener(null);
passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
protected void toggleShowPwd() {
if (passwordInput.getTransformationMethod() != null) {
passwordInput.setTransformationMethod(null);
passwordInput.setSelection(passwordInput.getText().length());
ivShowPassword.setImageResource(R.drawable.ic_hide_pwd);
} else {
passwordInput.setTransformationMethod(new PasswordTransformationMethod());
passwordInput.setSelection(passwordInput.getText().length());
ivShowPassword.setImageResource(R.drawable.icn_show_pwd);
}
}
private int getInputType(String type) {
int inputType;
switch (type) {
case Constants.Value.TEXT:
inputType = InputType.TYPE_CLASS_TEXT;
break;
case Constants.Value.DATE:
inputType = InputType.TYPE_NULL;
getHostView().setFocusable(false);
break;
case Constants.Value.DATETIME:
inputType = InputType.TYPE_CLASS_DATETIME;
break;
case Constants.Value.EMAIL:
inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case Constants.Value.PASSWORD:
inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
getHostView().setTransformationMethod(PasswordTransformationMethod.getInstance());
break;
case Constants.Value.TEL:
inputType = InputType.TYPE_CLASS_PHONE;
break;
case Constants.Value.TIME:
inputType = InputType.TYPE_NULL;
getHostView().setFocusable(false);
break;
case Constants.Value.URL:
inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI;
break;
case Constants.Value.NUMBER:
inputType = InputType.TYPE_CLASS_NUMBER;
break;
default:
inputType = InputType.TYPE_CLASS_TEXT;
}
return inputType;
}
private void setPwdVisible(boolean b) {
if (!b){
ivEye.setBackgroundResource(R.drawable.eye_close);
ivEye.setTag(false);
etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
}else{
ivEye.setBackgroundResource(R.drawable.eye_open);
ivEye.setTag(true);
etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
etPwd.invalidate();
}
protected void setupEditText(EditText editText) {
editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(filters);
editText.setOnTouchListener(touchListener);
editText.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}