效果图
布局代码
<androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tv_account_hint"style="@style/create_account_left"android:text="@string/team_set_account_string"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/ed_account"style="@style/create_account_edit_right"android:hint="@string/team_set_account_hint_string"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/tv_account_hint"app:layout_constraintTop_toTopOf="parent" /><Viewstyle="@style/create_account_line"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_account_hint" /><TextViewandroid:id="@+id/tv_password_hint"style="@style/create_account_left"android:text="@string/team_set_pwd_string"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_account_hint" /><EditTextandroid:id="@+id/ed_password"style="@style/create_account_edit_right"android:inputType="textPassword"android:hint="@string/team_set_password_hint_string"app:layout_constraintEnd_toStartOf="@+id/iv_pwd_eye"app:layout_constraintStart_toEndOf="@+id/tv_password_hint"app:layout_constraintTop_toBottomOf="@+id/ed_account" /><ImageViewandroid:id="@+id/iv_pwd_eye"android:layout_width="@dimen/dm048"android:layout_height="@dimen/dm048"android:layout_marginTop="@dimen/dm024"android:layout_marginRight="@dimen/dm024"android:scaleType="fitXY"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/ed_password"app:layout_constraintTop_toBottomOf="@+id/ed_account"tools:src="@drawable/vip_score_see_off" /><Viewstyle="@style/create_account_line"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_password_hint" /><TextViewandroid:id="@+id/tv_sure_hint"style="@style/create_account_left"android:text="@string/team_sure_pwd_string"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_password_hint" /><EditTextandroid:id="@+id/ed_password_again"style="@style/create_account_edit_right"android:hint="@string/team_again_pwd_string"android:inputType="textPassword"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/tv_sure_hint"app:layout_constraintTop_toBottomOf="@+id/ed_password" /><ImageViewandroid:id="@+id/iv_pwd_again_eye"android:layout_width="@dimen/dm048"android:layout_height="@dimen/dm048"android:layout_marginTop="@dimen/dm024"android:layout_marginRight="@dimen/dm024"android:scaleType="fitXY"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/ed_password"app:layout_constraintTop_toBottomOf="@+id/ed_password"tools:src="@drawable/vip_score_see_off" /></androidx.constraintlayout.widget.ConstraintLayout>
style 引用
<!--创建账号 右边--><style name="create_account_edit_right"><item name="android:layout_width">@dimen/dm000</item><item name="android:layout_height">@dimen/dm096</item><item name="android:layout_marginLeft">@dimen/dm040</item><item name="android:background">@null</item><item name="android:ellipsize">end</item><item name="android:gravity">center_vertical</item><item name="android:singleLine">true</item><item name="android:paddingRight">@dimen/dm024</item><item name="android:textColor">@color/color_999</item><item name="android:textSize">@dimen/dm032</item></style><!--创建账号 左边--><style name="create_account_left"><item name="android:layout_width">@dimen/dm162</item><item name="android:layout_height">@dimen/dm096</item><item name="android:gravity">center_vertical</item><item name="android:textColor">@color/color_19</item><item name="android:layout_marginLeft">@dimen/dm032</item><item name="android:textSize">@dimen/dm032</item></style>
代码
//设置默认图setSye(binding.ivPwdEye, false)setSye(binding.ivPwdAgainEye, false)//点击事件binding.ivPwdEye.setOnClickListener {setEysSee(binding.edPassword, binding.ivPwdEye)}binding.ivPwdAgainEye.setOnClickListener {setEysSee(binding.edPasswordAgain, binding.ivPwdAgainEye)}/*** 密码是否可见* 1 明文*/private fun setEysSee(editText: EditText, imageView: ImageView) {if (editText.inputType == 1) {editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT} else {editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_NORMAL}setSye(imageView, editText.inputType == 1)//光标位置editText.setSelection(editText.text.toString().length)}private fun setSye(imageView: ImageView, isSee: Boolean) {imageView.background = resources.getDrawable(if (isSee) R.drawable.vip_score_see_on else R.drawable.vip_score_see_off)}
就可以实现OK了。