Android 即使孩子在家长触摸事件中,如何调用家长触摸事件?
我有一个线性布局和一个按钮。我需要截获家长触摸监听器,即使我触摸按钮上方/上方。 这是我的密码:-Android 即使孩子在家长触摸事件中,如何调用家长触摸事件?,android,Android,我有一个线性布局和一个按钮。我需要截获家长触摸监听器,即使我触摸按钮上方/上方。 这是我的密码:- <LinearLayout android:id="@+id/button_privacy_layout" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@d
<LinearLayout
android:id="@+id/button_privacy_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/wysiwyg_privacy_selector"
android:paddingBottom="@dimen/ten"
android:paddingLeft="@dimen/twenty"
android:paddingRight="@dimen/ten"
android:paddingTop="@dimen/ten" >
<Button
android:id="@+id/buttonPrivacy"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="@drawable/ic_privacy"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
我可以通过某些属性使用xml实现它吗?尝试在父
onTouch
方法中返回false
,例如:
public boolean onTouch(View v, MotionEvent event) {
//Some code
return false;
}
您需要为LinearLayout设置OnTouchListener,并在onTouch方法中返回false
buttonPrivacyLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
//buttonPrivacyLayout view was clicked
return false;
}
});
如果onTouch方法返回false,则than按钮也可以处理OnTouchListener尝试以下方法:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/parent"
android:layout_width="100dip"
android:layout_height="50dip" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/parent2"
android:layout_width="100dip"
android:layout_height="50dip" >
</LinearLayout>
然后单击parent2布局否您不能这样做