如何消耗父视图的touchlistener子视图的触摸事件? [英] How to consume child view's touch event in parent view's touchlistener?
问题描述
在我的应用程序,我想所有子视图的触摸事件在我父视图的 onTouchListener
,但我不能得到这个。
In my application I want to get the touch event of all child view's in my parent view's onTouchListener
but I could not get this.
示例:
在我的活动的看法我有一个框架布局其中有一些按钮和edittexts在里面。所以每当我触摸式按键或编辑文本我想在这个触摸事件的 framlayout的 onTouchListeners
。
In my activity's view I have one frame layout which has some buttons and edittexts in it. So whenever I touch buttons or edit text I want to get this touch event in framlayout's onTouchListeners
.
下面是我的code ..
Here is my code..
活动:
private FrameLayout rootView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rootView = (FrameLayout) findViewById(R.id.rootview);
rootView.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
Log.e("Touch Event: ", " X: " + event.getX() + " Y: " + event.getY());
return false;
}
});
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:id="@+id/rootview">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="4dp"
android:text="Login"
android:textColor="@android:color/black"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Username"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" />
<EditText
android:id="@+id/edttextusername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:singleLine="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" />
<EditText
android:id="@+id/edttextpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:password="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >
<Button
android:id="@+id/btnlogin"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:enabled="false"
android:text="Login"
android:padding="5dip"
android:textColor="@android:color/black"
android:textStyle="bold"
/>
<Button
android:id="@+id/btncall"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:padding="5dip"
android:text="Cancel"
android:textColor="@android:color/black"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
问题:所以whenver我触摸按钮或EditText上我不能触摸的合作ordition在我的框架布局的onTouchListener(它不叫)
Problem: So whenver I touch the buttons or edittext I can't get touch co-ordition in my frame layout's onTouchListener (It's not called).
注意:我不希望添加独立onTouchListener所有childViews
Note: I don't want to add separate onTouchListener for all childViews.
在此先感谢。
推荐答案
您可以实现这一目标通过重写<一href="http://developer.android.com/reference/android/view/ViewGroup.html#dispatchTouchEvent%28android.view.MotionEvent%29"><$c$c>dispatchTouchEvent$c$c>在布局
You could accomplish that by overriding dispatchTouchEvent
in the layout.
public class MyFrameLayout extends FrameLayout {
@Override
public boolean dispatchTouchEvent(MotionEvent e) {
// do what you need to with the event, and then...
return super.dispatchTouchEvent(e);
}
}
然后使用该布局代替通常的FrameLayout的:
Then use that layout in place of the usual FrameLayout:
<com.example.android.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:id="@+id/rootview">
...
您也可以跳过超级调用整个的,如果你需要prevent子视图接收事件,但我认为这将是罕见的。如果需要重新创建的默认功能一些,但不是全部,有很多重写:
You could also skip the super call entirely if you need to prevent child views from receiving the event, but I think this would be rare. If you need to recreate some, but not all, of the default functionality, there is a lot to rewrite:
<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/view/ViewGroup.java#ViewGroup.dispatchTouchEvent%28android.view.MotionEvent%29">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/view/ViewGroup.java#ViewGroup.dispatchTouchEvent%28android.view.MotionEvent%29
这篇关于如何消耗父视图的touchlistener子视图的触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!