可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In my application, I have an EditText
whose default input type is set to android:inputType="textPassword"
by deault. It has a CheckBox
to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that is
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
My problem is, when that CheckBox is unchecked it should again set the input type to PASSWORD. I've done it using-
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
But, the text inside that edittext is still visible. And for surprise, when I change the orienatation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).
Any way to achieve this?
回答1:
Just for the people who are having same problem. Just add an extra attribute to that EditText programmatically and you are done.
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
Also, make sure that the cursor is at the end of the text in the EditText. Because, when you change the input type, the cursor will be automatically set to the starting point. So I suggest to use the following code:
et_password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
et_password.setSelection(et_password.getText().length());
When using Data Binding, you can make use of the following code
<data>
<import type="android.text.InputType"/>
.
.
.
<EditText
android:inputType='@{someViewModel.isMasked ?
(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) :
InputType.TYPE_CLASS_TEXT }'
If using Kotlin,
password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
回答2:
use this code to change password to text and vice versa
mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
for full sample code refer http://www.codeproject.com/Tips/518641/Show-hide-password-in-a-edit-text-view-password-ty
回答3:
password.setInputType(InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD);
Method above didn't really work for me. Answer below works for 2.2 sdk.
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
Set inputType for an EditText?
回答4:
Another simple example using ImageView to toggle visibility with less code, because of single InputType assign we need only equality operator:
EditText inputPassword = (EditText) findViewById(R.id.loginPassword);
ImageView inputPasswordShow = (ImageView) findViewById(R.id.imagePasswordShow);
inputPasswordShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(inputPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
inputPassword.setInputType( InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
}else {
inputPassword.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
}
inputPassword.setSelection(inputPassword.getText().length());
}
});
Replacing :
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
With :
InputType.TYPE_CLASS_TEXT
Will give the same result but shorter word.
回答5:
Checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is checked.
if (isChecked) {
//password is visible
PasswordField.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//password gets hided
passwordField.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
回答6:
Ok So after hours of trying finally implemented it. Below is the code ..
buttons.get(2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(buttons.get(2).getText().toString().equalsIgnoreCase(getResources().getString(R.string.show))){
editTexts.get(1).setInputType(InputType.TYPE_CLASS_TEXT);
editTexts.get(1).setSelection(editTexts.get(1).getText().length());
buttons.get(2).setText(getResources().getString(R.string.hide));
}else{
editTexts.get(1).setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
//editTexts.get(1).setTransformationMethod(PasswordTransformationMethod.getInstance());
editTexts.get(1).setSelection(editTexts.get(1).getText().length());
buttons.get(2).setText(getResources().getString(R.string.show));
}
}
});
Explanations:- I have a button with default text as show.
After onclick event on it checking if button's text is show.
If it is show then changing the input type,adjusting the cursor position and setting new text as hide in it.
When it is hide... doing reverse i.e. hiding the password,adjusting the cursor and setting the text as show. And that's it. It is working like a charm.
回答7:
This worked for me:
mytext.setInputType(InputType.TYPE_CLASS_NUMBER);
回答8:
Use this code to change password to text and vice versa.
This code perfectly worked for me.
Try this..
EditText paswrd=(EditText)view.findViewById(R.id.paswrd);
CheckBox showpass=(CheckBox)view.findViewById(R.id.showpass);
showpass.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
paswrd.setInputType(InputType.TYPE_CLASS_TEXT);
}else{
paswrd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
回答9:
For kotlin users:
password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
回答10:
This is the full onClick handler for the Image/Button to show/hide the password.
new OnClickListener() {
@Override
public void onClick(View v) {
// current ursor position
int cursorPosition = edtPassword.getSelectionStart();
// toggles the control variable
isPassworsVisible = !isPassworsVisible;
// sets the image toggler inside edit text
passwordVisible.setImageDrawable(getResources().getDrawable(isPassworsVisible ? R.drawable.ic_eye_checked : R.drawable.ic_eye_unchecked));
// apply input type
edtPassword.setInputType(isPassworsVisible ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
// returns cursor to position
edtPassword.setSelection(cursorPosition);
}
};
回答11:
The Password Visibility Toggle feature has been added to support library version 24.2.0 enabling you to toggle the password straight from the EditText
without the need for a CheckBox
.
You can make that work basically by first updating your support library version to 24.2.0 and then setting an inputType
of password on the TextInputEditText
. Here's how to do that:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
You can get more information about the new feature on the developer documentation for TextInputLayout.
回答12:
My search for a similar solution for Visual Studio / Xamarin lead me to this thread. Below is what worked for me with Xamarin. Note that this implementation retains the TYPE_TEXT_FLAG_NO_SUGGESTIONS
flag when switching between modes.
EditText et = FindViewById<EditText>(Resource.Id.ET);
To show characters:
et.InputType = Android.Text.InputTypes.TextVariationVisiblePassword | Android.Text.InputTypes.TextFlagNoSuggestions;
To hide characters:
et.InputType = Android.Text.InputTypes.TextVariationPassword | Android.Text.InputTypes.ClassText;
To set position to end:
int position = et.Text.Length;
et.SetSelection(position, position);
回答13:
some dynamic situation holder.edit_pin.setInputType(InputType.TYPE_CLASS_NUMBER);
will not work so better use both like that
holder.edit_pin.setInputType(InputType.TYPE_CLASS_NUMBER);
holder.edit_pin.setTransformationMethod(PasswordTransformationMethod.getInstance());
Note : this is suitable for when you are using dynamic controls like using arrayaapter
回答14:
Since the Support Library v24.2.0. you can achivie this very easy
What you need to do is just:
Add the design library to your dependecies
dependencies {
compile "com.android.support:design:25.1.0"
}
Use TextInputEditText
in conjunction with TextInputLayout
<android.support.design.widget.TextInputLayout
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_hint"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
passwordToggleEnabled
attribute will make the password toggle appear
In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto"
You can customize your password toggle by using:
app:passwordToggleDrawable
- Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint
- Icon to use for the password input visibility toggle.
app:passwordToggleTintMode
- Blending mode used to apply the background tint.
More details in TextInputLayout documentation.
回答15:
I change the input type on my checkbox, so on my OnCheckedChangeListener
i do:
passwordEdit.setInputType(InputType.TYPE_CLASS_TEXT| (isChecked? InputType.TYPE_TEXT_VARIATION_PASSWORD|~InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));
And it finally worked.
Seems like a boolean problem with TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
. Invert the flag and it should fix the problem.
In your case:
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|~InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
回答16:
I would remove android:inputType="textPassword"
from your layout. That is why it is switching back to password when the orientation changes. Because each time the orientation changes the view is being recreated.
As for the first problem try this:
String text = password.getText();
password.setText("");
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setText(text);
basically emptying out the text before you change the input type and then add it back.
回答17:
Just an additional comment on the correct answer provided by @Rajkiran, you may want to add
etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
to the NORMAL input state so that the users wont be annoyed by the keyboard's auto-suggestion
回答18:
Complete code when you want to apply the Password visibility in Password edit text.
Create a handle [ Any drawable or Checkbox]
on click or on Checked/Uncheck write this:
if (edittext.getInputType() == (InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD )){
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD );
}else{
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
}
Do not forget to write this line:
edittext.setSelection(edittext.getText().length());
It resets the cursor to the end of line.
回答19:
Blockquote
final int[] count = {0};
showandhide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(count[0] ==0)
{
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
count[0]++;
}
else {
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
showandhide.setText("Hide");
count[0]--;
}
}
});
回答20:
Based on answers of neeraj t and Everton Fernandes Rosario I wrote in Kotlin, where password
is an id of an EditText in your layout.
// Show passwords' symbols.
private fun showPassword() {
password.run {
val cursorPosition = selectionStart
transformationMethod = HideReturnsTransformationMethod.getInstance()
setSelection(cursorPosition)
}
}
// Show asterisks.
private fun hidePassword() {
password.run {
val cursorPosition = selectionStart
transformationMethod = PasswordTransformationMethod.getInstance()
setSelection(cursorPosition)
}
}
回答21:
After you setInputType
for a password field, you will have problem with FONT
Here is my solution for show/hide password without font problem
protected void onCreate(Bundle savedInstanceState) {
...
findViewById(R.id.button_show_hide_password).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPasswordVisible(edtPassword)) {
enableInputHiddenPassword(edtPassword);
} else {
enableInputVisiblePassword(edtPassword);
}
edtPassword.setSelection(edtPassword.getText().length());
}
});
}
final int INPUT_TYPE_VISIBLE_PASSWORD = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
final int INPUT_TYPE_HIDDEN_PASSWORD = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
private boolean isPasswordVisible(EditText editText) {
return editText.getInputType() == INPUT_TYPE_VISIBLE_PASSWORD;
}
private void enableInputVisiblePassword(EditText editText) {
Typeface cache = editText.getTypeface();
editText.setInputType(INPUT_TYPE_VISIBLE_PASSWORD);
editText.setTypeface(cache);
}
private void enableInputHiddenPassword(EditText editText) {
Typeface cache = editText.getTypeface();
editText.setInputType(INPUT_TYPE_HIDDEN_PASSWORD);
editText.setTypeface(cache);
}
Note: I use InputType.TYPE_TEXT_VARIATION_PASSWORD
instead of InputType.TYPE_CLASS_TEXT
or HideReturnsTransformationMethod
because I want the keyboard display both text and number
DEMO