【问题标题】:View.VISIBLE and INVISIBLE doesn't work KotlinView.VISIBLE 和 INVISIBLE 不起作用 Kotlin
【发布时间】:2020-03-26 16:55:14
【问题描述】:

我有密码的编辑文本和显示密码的按钮。仅当 edittext 不为空时,按钮才可见。我做了一些代码,但它不起作用(它总是不可见的)。这是代码:

passwordEditTextSUA.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            if (passwordEditTextSUA.text.toString() != "") {
                showPasswordSUA.visibility == View.VISIBLE
            } else {
                showPasswordSUA.visibility == View.INVISIBLE
            }
        }
    })

以及按钮和edittext的xml:

<EditText
        android:id="@+id/passwordEditTextSUA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginEnd="34dp"
        android:layout_marginBottom="35dp"
        android:hint="@string/enter_password"
        android:inputType="textPassword"
        android:theme="@style/EditTextTheme"
        app:layout_constraintBottom_toTopOf="@+id/repeatPasswordEditTextSUA"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

<ImageView
        android:id="@+id/showPasswordSUA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="1dp"
        android:background="@android:color/transparent"
        android:src="@drawable/button_show_password"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/passwordEditTextSUA"
        app:layout_constraintEnd_toEndOf="@+id/passwordEditTextSUA"
        app:layout_constraintTop_toTopOf="@+id/passwordEditTextSUA" />

怎么了?

【问题讨论】:

  • 什么不起作用?它是始终可见、始终不可见还是保持其当前状态无论是不可见还是可见?

标签: android kotlin android-edittext imageview


【解决方案1】:

showPasswordSUA.visibility = View.VISIBLE 替换这个showPasswordSUA.visibility == View.VISIBLE 我使用java,但我认为双等号只是评估为真或假。您只想使用一个=。见this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2016-12-26
    • 2023-03-29
    相关资源
    最近更新 更多