溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

EditText的密碼隱藏與顯示切換

發(fā)布時(shí)間:2024-08-21 17:31:21 來(lái)源:億速云 閱讀:80 作者:小樊 欄目:移動(dòng)開(kāi)發(fā)

可以通過(guò)設(shè)置EditText的inputType屬性來(lái)實(shí)現(xiàn)密碼的隱藏與顯示切換。當(dāng)inputType設(shè)置為"textPassword"時(shí),密碼會(huì)顯示為隱藏的圓點(diǎn);當(dāng)inputType設(shè)置為"textVisiblePassword"時(shí),密碼會(huì)顯示為明文。

示例代碼如下所示:

<EditText
    android:id="@+id/passwordEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="Password"/>

<Button
    android:id="@+id/showHideButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show/Hide Password"/>
Button showHideButton = findViewById(R.id.showHideButton);
showHideButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText passwordEditText = findViewById(R.id.passwordEditText);
        
        if (passwordEditText.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
            passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            passwordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        
        // 將光標(biāo)移動(dòng)到末尾
        passwordEditText.setSelection(passwordEditText.getText().length());
    }
});

以上代碼中,通過(guò)點(diǎn)擊按鈕來(lái)切換密碼的顯示方式。當(dāng)密碼為明文時(shí),點(diǎn)擊按鈕后會(huì)變?yōu)殡[藏的圓點(diǎn);當(dāng)密碼為隱藏的圓點(diǎn)時(shí),點(diǎn)擊按鈕后會(huì)變?yōu)槊魑摹?/p>

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI