簡體   English   中英

如何修復 TextInputLayout 密碼切換?

[英]How to fix TextInputLayout passwordToggle?

我創建了一個帶有驗證的簡單登錄頁面,但是當我想使用passwordToggleEnabled時,密碼部分出現問題,應用程序崩潰並且不知道為什么,因為我有 0 個錯誤。

我的項目在 API 26 上。

我在依賴項中添加了構建 gradle:

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'

在 XML 文件中,這是我的密碼輸入代碼:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/test2"
            android:layout_below="@id/usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            app:errorEnabled="true"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

在 MainActivity.java 這就是我如何調用 pwd 的 id:

String passwordInput = test2.getText().toString().trim();

所以這就是我如何實現我的代碼並且應用程序不斷停止所以我做錯了什么?

謝謝!

使用getEditText方法獲取用於文本輸入的EditText

TextInputLayout test2= findViewById(R.id.test2);
String passwordInput = test2.getEditText().getText().toString().trim();

您必須首先通過為其分配一個 ID 來找到視圖,如下所示

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

然后像這樣在你的活動中找到它

 TextInputEditText etPassword= findViewById(R.id.etPassword);

然后你可以得到輸入的文本,比如

String passwordInput = etPassword.getText().toString().trim();

為 TextInputEditText 分配一個 ID

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

然后像這樣在你的活動中找到它

EditText etPassword= findViewById(R.id.etPassword);

然后你可以得到輸入的文本,比如

String mPassword = etPassword.getText().toString().trim();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM