You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何在EditText内添加密码眼睛图标并调整尺寸?

Android EditText添加密码显示/隐藏图标并调整尺寸

一、解决图标尺寸过大的问题

直接用android:drawableRight引用图标时,图标会按原始尺寸显示,可通过以下两种方式调整:

1. 修改矢量图标文件尺寸(适用于VectorDrawable)

如果你的眼睛图标是xml格式的矢量图,直接在图标文件中修改宽高参数:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"  <!-- 设置目标宽度 -->
    android:height="24dp" <!-- 设置目标高度 -->
    android:viewportWidth="24" <!-- 保持与原始矢量图的viewport比例一致 -->
    android:viewportHeight="24">
    <!-- 图标路径内容 -->
</vector>

修改后再通过android:drawableRight引用,图标就会按设置的尺寸显示。

2. 代码中动态调整Drawable尺寸(适用于所有图标类型)

如果不想修改原始图标文件,可在代码中获取EditText的CompoundDrawable,手动设置其边界:

EditText passwordEt = findViewById(R.id.passwordEt);
// 获取右侧的Drawable(索引2对应右侧,0左、1上、3下)
Drawable rightDrawable = passwordEt.getCompoundDrawables()[2];
if (rightDrawable != null) {
    // 将dp转换为px,这里设置为24dp
    int iconSize = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        24,
        getResources().getDisplayMetrics()
    );
    // 设置Drawable的显示边界
    rightDrawable.setBounds(0, 0, iconSize, iconSize);
    // 重新设置EditText的CompoundDrawables
    passwordEt.setCompoundDrawables(
        passwordEt.getCompoundDrawables()[0],
        passwordEt.getCompoundDrawables()[1],
        rightDrawable,
        passwordEt.getCompoundDrawables()[3]
    );
}

二、实现密码显示/隐藏的点击功能

1. 布局文件设置

先在EditText中添加右侧图标:

<EditText
    android:id="@+id/passwordEt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:drawableRight="@drawable/ic_visibility" <!-- 初始显示眼睛图标 -->
    android:drawablePadding="8dp"/> <!-- 图标与文本的间距 -->

2. 代码中处理点击事件

给EditText设置点击监听,判断点击区域是否为右侧图标,然后切换密码显示状态和图标:

passwordEt.setOnClickListener(v -> {
    int currentInputType = passwordEt.getInputType();
    // 判断当前是否为隐藏密码状态
    if ((currentInputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) {
        // 切换为显示密码
        passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        passwordEt.setCompoundDrawablesWithIntrinsicBounds(null, null, getDrawable(R.drawable.ic_visibility_off), null);
    } else {
        // 切换为隐藏密码
        passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        passwordEt.setCompoundDrawablesWithIntrinsicBounds(null, null, getDrawable(R.drawable.ic_visibility), null);
    }
    // 将光标移到文本末尾,避免切换后光标错位
    passwordEt.setSelection(passwordEt.getText().length());
});

3. 更简洁的方案:使用TextInputLayout

如果你使用Material Design组件,推荐用TextInputLayout配合EditText,自带密码切换功能,且图标尺寸更易调整:

布局文件:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/passwordTil"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密码"
    app:endIconDrawable="@drawable/ic_visibility"
    app:endIconMode="custom">

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

</com.google.android.material.textfield.TextInputLayout>

代码处理:

TextInputLayout passwordTil = findViewById(R.id.passwordTil);
passwordTil.setEndIconOnClickListener(v -> {
    EditText passwordEt = passwordTil.getEditText();
    if (passwordEt == null) return;

    int inputType = passwordEt.getInputType();
    if ((inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) {
        passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        passwordTil.setEndIconDrawable(R.drawable.ic_visibility_off);
    } else {
        passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        passwordTil.setEndIconDrawable(R.drawable.ic_visibility);
    }
    passwordEt.setSelection(passwordEt.getText().length());
});

调整TextInputLayout的图标尺寸:

通过自定义style设置:

<style name="CustomTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="endIconSize">24dp</item> <!-- 设置图标大小 -->
</style>

然后给TextInputLayout添加style="@style/CustomTextInputLayoutStyle"即可。

内容的提问来源于stack exchange,提问作者Abhirami G S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:40:31