【问题标题】:TextInputEditText - Make clickable but not editableTextInputEditText - 使可点击但不可编辑
【发布时间】:2021-03-05 17:15:04
【问题描述】:

我有一个布局,我想在其中显示一组编辑文本和一个日期选择器。由于我想对所有字段进行相同的设计,我发现我也需要为日期选择器使用编辑文本,但使其不可编辑但可点击。下面是我的编辑文本的 xml 代码,用于显示日期选择器:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notification_layout"
    style="@style/Theme.Connect.InputFieldLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:startIconDrawable="@drawable/ic_stk_notification_24dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notification_edit_text"
        style="@style/Theme.Connect.InputField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Notification"
        android:maxLines="1"
        android:lines="1"
        android:cursorVisible="false"/>

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

我尝试了许多在互联网上找到的解决方案,我确实设法使编辑文本不可编辑,但没有波纹点击效果。有没有办法使编辑文本不可编辑但仍保持波纹点击效果?这是显示包含表单布局设计的日期选择器的最佳做法吗?

【问题讨论】:

    标签: android layout material-ui material-components-android


    【解决方案1】:
     Try this and change the `android:focusable` attribute to `false`                       
    
    
    <com.google.android.material.textfield.TextInputLayout
          android:id="@+id/notification_layout"
           style="@style/Theme.Connect.InputFieldLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:startIconDrawable="@drawable/ic_stk_notification_24dp">
    
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notification_edit_text"
        style="@style/Theme.Connect.InputField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Notification"
        android:maxLines="1"
        android:focusable="false"
        android:lines="1"
        android:cursorVisible="false"/>
    
    </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

    • 感谢您的回答。试着回答,我仍然无法像通常的 MaterialAutoCompleteTextView 那样实现涟漪效果。
    • 如果您可以发送一份您想做的事情的样本,那将更有帮助
    【解决方案2】:

    您可以简单地使用TextView 而不是EditText,并且可以为其设置onClickListener()。您也可以按照自己的方式设置样式。

    notification_textview.setOnClickListener {
        // Whatever you want to do after a click
    }
    

    您可以通过将这 2 个属性添加到您的 textview 来获得触摸涟漪效果

    <......Textview
    android:background=?android:attr/selectableItemBackground
    android:clickable="true" />
    

    您还可以将样式添加到要用于editText 的文本视图。

    【讨论】:

    • 感谢您的回答。如果我使用文本视图,我最终会得到不同的设计字段。另外,文本视图不会有波纹点击效果,对吧?
    • @George 您可以为 textview 使用自定义样式,您需要做更多的研究才能做到这一点,
    • 在 TextInputEditText 中使用了android:background=?android:attr/selectableItemBackground,得到了涟漪效应。谢谢。
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多