2011-05-23 104 views
40

我需要在我的android应用程序中创建一个带有'username''password'字段和两个按钮'login'和'cancel'的登录表单。Android:如何在编辑文本中设置密码属性?

我正在使用带有edittext的警报对话框。

这是我用来创建密码的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); 

我的问题是,纯文本显示,而不是“点”当我打开一个softkeypad编辑密码。 (不在softkeypad模式下显示为点)

任何人都可以提出解决方案吗?

+14

您的会员命名风格会给您带来麻烦。如果'Password'是一个真正存在的类,那么上帝会提防静态方法。 – 2011-05-23 09:02:44

+0

我试过重命名..仍然没有运气 – Dhanesh 2011-05-23 09:25:14

+0

我明白..非常感谢.. :) – Dhanesh 2011-05-23 09:35:31

回答

82
Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

这一个作品为了我。
但是你必须看看Octavian Damiean的评论,他是对的。

+0

非常感谢ernazm ..这一个工作正常。我有更多的qn ..我的“Password.setHint(”密码“);”现在似乎不工作。任何想法? – Dhanesh 2011-05-23 09:37:02

+0

奇怪。试图将'setHint'与'setInputType'结合起来,它对我来说工作得很好。这是我的代码:'EditText Password =((EditText)findViewById(R.id.editText1)); Password.setHint(“Password”); Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);' – ernazm 2011-05-23 09:44:11

+0

[Here](http://stackoverflow.com/a/22255310/426227)是一些解释你为什么需要'TYPE_CLASS_TEXT'和'TYPE_TEXT_VARIATION_PASSWORD'的原因。 – testing 2017-04-03 11:23:07

3

请参阅此链接 text view android:password

这适用于EditText为好,因为它是TextView已知直接子类。

+1

@PareshMayani,对不起,我的朋友,但EditText是TextView的一个子类。现在,请停止“-1” - 你没有意识到或者你认为是错误的一切 – sfat 2012-05-10 07:39:47

+0

@PareshMayani和评论有什么意义?如果你觉得这不是直截了当的,你可能会改进我的答案 – sfat 2012-05-10 09:23:36

22

这已被弃用

在EditText上的XML iclude这个属性:机器人:密码= “真”

编辑

android:inputType="textPassword" 
+0

你忘记了什么吗? – Reno 2011-05-23 09:08:17

+0

我想用代码来做。 – Dhanesh 2011-05-23 09:19:26

+4

android:password =“true”已被弃用 – dhiku 2013-11-11 16:03:01

3

我发现这样做时,为了将引力设置为居中,并且仍然有密码提示,当using inputType时,android:gravity="Center"必须位于XML行的末尾。

<EditText android:textColor="#000000" android:id="@+id/editText2" 
    android:layout_width="fill_parent" android:hint="Password" 
    android:background="@drawable/rounded_corner" 
    android:layout_height="fill_parent" 
    android:nextFocusDown="@+id/imageButton1" 
    android:nextFocusRight="@+id/imageButton1" 
    android:nextFocusLeft="@+id/editText1" 
    android:nextFocusUp="@+id/editText1" 
    android:inputType="textVisiblePassword" 
    android:textColorHint="#999999" 
    android:textSize="16dp" 
    android:gravity="center"> 
</EditText> 
5

您需要使用PasswordTransformationMethod.getInstance(),而不是new PasswordTransformationMethod()

10

这里是把圆点密码的一种新的方式

<EditText 
    android:id="@+id/loginPassword" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:hint="@string/pwprompt"/

添加安卓的inputType =“textPassword”

+0

它似乎是xml声明的推荐方式。 http://developer.android.com/training/keyboard-input/style.html – Loenix 2015-12-05 10:15:08

4

仅使用代码(而不是XML)为我工作的方式是这样的一个:

etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
0

我搜索了类似的解决方案为Visual Studio 2015/Xamarin导致我这个线程。在设置EditText.InputTypeAndroid.Text.InputTypes.TextVariationVisiblePassword时,在用户输入过程中正确隐藏了密码,但如果密码在EditText Layout呈现之前(用户提交密码输入之前)可见,则不会“隐藏”该密码。为了在用户提交密码并且呈现EditText布局后隐藏可见的密码,我使用LuxuryMode建议的EditText.TransformationMethod = PasswordTransformationMethod.Instance

0

要在EditText中设置启用的密码,我们必须在xml文件中设置“inputType”属性。如果我们只使用EditText,那么我们将在EditText中设置输入类型,如下面的代码所示。

    <EditText 
        android:id="@+id/password_Edit" 
        android:focusable="true" 
        android:focusableInTouchMode="true" 
        android:hint="password" 
        android:imeOptions="actionNext" 
        android:inputType="textPassword" 
        android:maxLength="100" 
        android:nextFocusDown="@+id/next" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 

密码启用属性是

android:inputType="textPassword" 

但是,如果我们要实现与材料设计(设计支持库)密码的EditText那么我们将不得不写代码给出波纹管。

<android.support.design.widget.TextInputLayout 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/txtInput_currentPassword" 
       android:layout_width="match_parent" 
       app:passwordToggleEnabled="false" 
       android:layout_height="wrap_content"> 

       <EditText 
        android:id="@+id/password_Edit" 
        android:focusable="true" 
        android:focusableInTouchMode="true" 
        android:hint="@string/hint_currentpassword" 
        android:imeOptions="actionNext" 
        android:inputType="textPassword" 
        android:maxLength="100" 
        android:nextFocusDown="@+id/next" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
      </android.support.design.widget.TextInputLayout> 

@注意: - 在Android SDK 24及更高版本中,“passwordToggleEnabled”默认为true。因此,如果我们在密码EditText中有海关处理显示/隐藏功能,那么我们必须在代码中将其设置为false,如上所示。

app:passwordToggleEnabled="true" 

要添加上面的行,我们必须在根布局中添加下面的行。

xmlns:app="http://schemas.android.com/apk/res-auto" 
相关问题