Android:如何在编辑文本中设置密码属性?
我需要在我的 Android 应用程序中创建一个包含“用户名”“密码”字段和两个按钮“登录”和“取消”的登录表单。
我正在使用一个警报对话框,其中包含编辑文本。
这是我用来创建密码 edittext 的代码。
final EditText Password = new EditText(this);
Password.setGravity(Gravity.CENTER);
Password.setHint("Password");
Password.setWidth(200);
Password.setTransformationMethod(new PasswordTransformationMethod());
login_alert.addView(Password);
我的问题是,当我打开软键盘编辑密码时,显示纯文本而不是“点”。 (不在软键盘模式下时显示为点)
任何人都可以提出解决方案吗?
I need to create a login form with 'username' 'password' fields and two buttons 'login' and 'cancel' in my android application.
I am using an alert dialog with edittext inside that.
This is the code I used to create password edittext..
final EditText Password = new EditText(this);
Password.setGravity(Gravity.CENTER);
Password.setHint("Password");
Password.setWidth(200);
Password.setTransformationMethod(new PasswordTransformationMethod());
login_alert.addView(Password);
My issue is that, plain text is shown instead of 'dots' when i open a softkeypad to edit the password. (It is shown as dots when not in softkeypad mode)
Can anyone suggest a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
请参阅此链接
文本视图 android:password
这也适用于
EditText
,因为它是TextView
的已知直接子类。See this link
text view android:password
This applies for
EditText
as well, as it is a known direct subclass ofTextView
.我发现在执行此操作时,为了将重力设置为中心,并且在
使用 inputType
时仍然显示密码提示,android:gravity="Center"
必须是在 XML 行的末尾。I found when doing this that in order to set the gravity to center, and still have your password hint show when
using inputType
, theandroid:gravity="Center"
must be at the end of your XML line.要设置在 EditText 中启用的密码,我们必须在 xml 文件中设置“inputType”属性。如果我们仅使用 EditText,那么我们将在 EditText 中设置输入类型,如下面的代码所示。
密码启用属性是
但是如果我们使用 Material Design(带有设计支持库)实现密码 EditText,那么我们将编写如下代码。
@注意:- 在 Android SDK 24 及更高版本中,“passwordToggleEnabled”默认为 true。因此,如果我们在密码 EditText 中对显示/隐藏功能进行海关处理,那么我们必须在上面给出的代码中将其设置为 false。
要添加上面的行,我们必须在根布局中添加下面的行。
To set password enabled in EditText, We will have to set an "inputType" attribute in xml file.If we are using only EditText then we will have set input type in EditText as given in below code.
Password enable attribute is
But if we are implementing Password EditText with Material Design (With Design support library) then we will have write code as given bellow.
@Note: - In Android SDK 24 and above, "passwordToggleEnabled" is by default true. So if we have the customs handling of show/hide feature in the password EditText then we will have to set it false in code as given above in .
To add above line, we will have to add below line in root layout.
我对 Visual Studio 2015/Xamarin 的类似解决方案的搜索引导我找到了这个线程。将
EditText.InputType
设置为Android.Text.InputTypes.TextVariationVisiblePassword
可以在用户输入期间正确隐藏密码,但如果密码在输入之前可见,则不会“隐藏”密码。 EditText 布局已呈现(在用户提交密码条目之前)。为了在用户提交密码并呈现 EditText 布局后隐藏可见的密码,我按照 LuxuryMode 的建议使用了EditText.TransformationMethod = PasswordTransformationMethod.Instance
。My search for a similar solution for Visual Studio 2015/Xamarin lead me to this thread. While setting the
EditText.InputType
toAndroid.Text.InputTypes.TextVariationVisiblePassword
properly hid the password during user entry, it did not 'hide' the password if it was visible before the EditText Layout was rendered (before the user submitted their password entry). In order to hide a visible password after the user submits their password and the EditText Layout is rendered, I usedEditText.TransformationMethod = PasswordTransformationMethod.Instance
as suggested by LuxuryMode.这个对我有用。
但你必须看看奥克塔维安·达米安的评论,他是对的。
This one works for me.
But you have to look at Octavian Damiean's comment, he's right.
这已被弃用
在 EditText 的 xml 中包含此属性: android:password="true"
编辑
This is deprecated
In xml of EditText iclude this attribute: android:password="true"
Edit
这是在密码中添加点的新方法
add android:inputType = "textPassword"
Here's a new way of putting dots in password
add android:inputType = "textPassword"
您需要使用
PasswordTransformationMethod.getInstance()
而不是new PasswordTransformationMethod()
。You need to use
PasswordTransformationMethod.getInstance()
instead ofnew PasswordTransformationMethod()
.使用代码(而不是 XML)对我来说唯一有效的方法是:
The only way that worked for me using code (not XML) is this one: