如果子视图占用整个父级宽度和高度,如何使 FrameLayout 父级可点击?
How to make FrameLayout parent clickable if child views are taking whole parent width and height?
我有使用 TextInputEditText
和 TextInputLayout
的特殊布局。它的形式像布局,但一个 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>
你能试试下面的自定义 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
for editText 因为 editText 正在获取焦点。
我有使用 TextInputEditText
和 TextInputLayout
的特殊布局。它的形式像布局,但一个 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>
你能试试下面的自定义 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
for editText 因为 editText 正在获取焦点。