【问题标题】:How to make FrameLayout parent clickable if child views are taking whole parent width and height?如果子视图占用整个父级宽度和高度,如何使 FrameLayout 父级可点击?
【发布时间】:2019-08-13 09:03:27
【问题描述】:

我有使用TextInputEditTextTextInputLayout 的特殊布局。它的形式类似于布局,但一个View 从外观上看只有TextInputEditText。它不会有任何可编辑的部分。它的目的是提供信息并像Button 一样行事。还有一个问题。我将这个EditText 包裹在FrameLayout 中,FrameLayout 就像Button 一样有onClickListener。问题是它对点击没有反应。如何启用它? (就可点击区域而言,FrameLayout 应该在其子项的前面)

<FrameLayout
   android:id="@+id/updateButton"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:focusable="true">

       <com.google.android.material.textfield.TextInputLayout
           android:id="@+id/etLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           style="@style/form_edit_text_style"
           app:errorEnabled="true"
           android:clickable="false"
           android:focusable="false"
           android:hint="@string/et_hint_num">


            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/etField"
                android:layout_width="match_parent"
                android:inputType="text"
                android:enabled="false"
                android:clickable="false"
                android:focusable="false"
                style="@style/form_inner_style"/>

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

</FrameLayout>

【问题讨论】:

    标签: android android-layout android-edittext android-textinputlayout


    【解决方案1】:

    你可以试试下面的自定义 FrameLayout:-

    public class MyFrameLayout extends FrameLayout {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // true if you do not want the children to be clickable.
            return mShouldInterceptAllTouch;
        }
    }
    

    另外,试试:-

    focusableInTouchMode=false 用于 editText,因为 editText 正在占据焦点。

    【讨论】:

    • 这只有在我单击此按钮一次时才有效。当我从上一个活动返回时,视图不再可点击。
    • 你是否从这个方法返回了真值?另外,您是否为框架布局设置了点击监听器?
    • 是的,我创建了自定义类,我将 true 作为常量返回。
    • 好的,已修复。当我从一个活动切换到另一个活动时,isEnabled 设置为 false。
    • 哦。好的。没问题
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多