2011-05-23 99 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" 
相關問題