Как использовать событие касания дочернего представления в touchlistener родительского представления?

В моем приложении я хочу получить событие касания всех дочерних представлений в моем родительском представлении onTouchListener , но я не мог получить это .

Пример:

В моем представлении активности у меня есть один макет кадра, в котором есть несколько кнопок и тексты редактирования. Поэтому всякий раз, когда я касаюсь кнопок или редактирую текст, я хочу получить это событие касания в framlayoutonTouchListeners.

Вот мой код..

Активность:

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> 

Проблема: Поэтому всякий раз, когда я касаюсь кнопок или редактирую текст, я не могу получить координацию касания в моем макете кадра. onTouchListener (он не вызывается).

Примечание: Я не хочу добавлять отдельный onTouchListener для всех дочерних представлений.

Заранее спасибо.

19
задан Sock 6 April 2012 в 10:19
поделиться