【问题标题】:How To Make TextInputEditText Read-only?如何使 TextInputEditText 只读?
【发布时间】:2019-09-01 17:55:42
【问题描述】:

我正在尝试将 TextInputEditText 设为只读,但光标快速闪烁并触发了点击。

我尝试设置isFocusable = falseisClickable = false

XML 布局:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/label_address"
    android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"/>

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

只读代码:

fun TextInputEditText.disable() {
    isFocusable = false
    isClickable = false
}

将TextInputEditText设为只读的正确方法或正确方法是什么?

【问题讨论】:

    标签: android kotlin material-design material-components-android


    【解决方案1】:

    我一般用setEnabled(boolean)ref link

    它可能满足您的需求吗?

    【讨论】:

      【解决方案2】:

      在 XML 中

      设置属性android:enable="false"in TextInputEditText

      代码

      <com.google.android.material.textfield.TextInputLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="Hints"
          android:layout_marginTop="10dp"
          android:layout_margin="40dp">
          <com.google.android.material.textfield.TextInputEditText
              android:id="@+id/input_address"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:enabled="false"
              android:imeOptions="actionNext"
              android:text="Hi How are you"
              android:textColor="@color/colorPrimary"
              android:textColorHint="@color/colorPrimary" />
      </com.google.android.material.textfield.TextInputLayout>
      

      结果:

      【讨论】:

        【解决方案3】:

        尝试将Edittext.setEnabled(false);android:editable="false" 添加到您的视图中。

        【讨论】:

        • 只是一个更新 - 此组件不推荐使用 android:editable
        【解决方案4】:

        XML 代码

        您可以尝试使用此属性:

        android:textIsSelectable="true"  
        android:inputType="none" 
        

        请注意,android:editable弃用

        Kotlin 代码

        假设你的变量名是editText,你可以试试这个:

        editText.isEnabled = false
        

        【讨论】:

          【解决方案5】:

          正确的方法是使用 TextView,您可以将它应用到您的 EditTextStyle。但是您不应该实现 EditText 并将其用作 TextView,请记住对象旨在满足特定需求,因此请始终尝试根据您的需要找到最适合的对象。

          【讨论】:

            【解决方案6】:

            在edittext中使用这些属性

            android:clickable="false"  android:cursorVisible="false" android:focusable="false" android:focusableInTouchMode="false"
            

            【讨论】:

              【解决方案7】:

              为您的 TextInputEditText 尝试 isEnabled 函数。

              input_address.isEnabled = false

              【讨论】:

                猜你喜欢
                • 2021-12-17
                • 2018-08-07
                • 2017-12-19
                • 2019-12-22
                • 1970-01-01
                • 2018-01-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多