【问题标题】:How to set onTouch for a layout(XML) that is added via inflation?如何为通过通货膨胀添加的布局(XML)设置 onTouch?
【发布时间】:2015-05-27 13:19:22
【问题描述】:

我试图让控制按钮在点击屏幕时弹出/关闭屏幕。他们将覆盖一个视频。我遇到的问题是如何为已膨胀的布局创建触摸事件。
例程如下:
1)给视频设置初始内容;
2) 在视频顶部包含控件的膨胀布局(XML);
3)(需要做的)为新膨胀的布局创建 onTouch 事件,当用户点击屏幕时隐藏/显示按钮。

包含显示/隐藏按钮的布局:

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/dpad_grid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:columnCount="3"
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/tiltUp"
            android:background="@drawable/arrow_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_row="0"
            android:layout_column="1"/>

        <Button
            android:id="@+id/panRight"
            android:background="@drawable/arrow_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_row="1"
            android:layout_column="2"/>

        <Button
            android:id="@+id/tiltDown"
            android:background="@drawable/arrow_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="8dp"
            android:layout_row="2"
            android:layout_column="1"/>

        <Button
            android:id="@+id/panLeft"
            android:background="@drawable/arrow_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_row="1"
            android:layout_column="0"/>

    </GridLayout>

    <Button
        android:id="@+id/zoomIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:background="@drawable/zoom_in"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:id="@+id/zoomOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/zoom_out"
        android:layout_marginRight="0dp"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/zoomIn"/>

    <Button
        android:id="@+id/irisDarker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/iris_darker"/>

    <Button
        android:id="@+id/irisBrighter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@+id/irisDarker"
        android:background="@drawable/iris_brighter"/>

    <Button
        android:id="@+id/focusFar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/dpad_grid"
        android:layout_marginRight="20dp"
        android:layout_alignParentStart="true"
        android:background="@drawable/focus_far"/>

    <Button
        android:id="@+id/focusNear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/focus_near"
        android:layout_alignTop="@+id/focusFar"
        android:layout_alignStart="@+id/irisBrighter"/>

    <Button
        android:id="@+id/snapshot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/camera"
        android:layout_alignEnd="@+id/focusFar"
        android:layout_alignParentStart="true"/>

    <Button
        android:id="@+id/settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/gear"/>

</RelativeLayout>

添加视频和膨胀上述布局的代码:

@Override
    public void onResume() {
        super.onResume();    

        RelativeLayout.LayoutParams relativeLayoutParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

        mv = new MjpegView(this);
        setContentView(mv);

        LayoutInflater inflater = getLayoutInflater();
        getWindow().addContentView(inflater.inflate(R.layout.screen2, null), relativeLayoutParams);

感谢您的帮助!

编辑:这是我目前正在使用的。为什么我的代码在触摸屏幕时没有进入触摸事件?

LayoutInflater inflater = getLayoutInflater();
        control_buttons = inflater.inflate(R.layout.screen2, null);

        control_buttons.setOnTouchListener(new View.OnTouchListener() {
            boolean gone = true;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                System.out.println("In OnTouch");

                if ( (event.getAction() == MotionEvent.ACTION_DOWN) && (gone == true) ){

                    getWindow().addContentView(control_buttons, relativeLayoutParams);

                    gone = false;
                    return true;
                }

                else if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == false)) {
                    v.setVisibility(View.GONE);
                    gone = true;
                    return true;
                }
                return false;
            }
        });

【问题讨论】:

  • 触摸事件?你是说 MotionEvents (developer.android.com/reference/android/view/MotionEvent.html)?
  • 我认为我不需要像滑动这样的所有动作。我只想在用户点击屏幕时显示/隐藏按钮。
  • 你读过 inflater.inflate() 返回的内容吗?
  • 我很确定它会返回一个视图,对吧?
  • 那么将触摸事件侦听器附加到该视图有什么问题?

标签: java android xml android-layout layout-inflater


【解决方案1】:

这可能更适合您。这仅在您的 RelativeLayout 中的子项是 Button 或包含 Button 的 GridLayout 时才有效。

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
    relativeLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                for(int i = 0; i < relativeLayout.getChildCount(); i++) {
                    if(relativeLayout.getChildAt(i) instanceof Button) {
                        Button temp = (Button) relativeLayout.getChildAt(i);
                        if(temp.getVisibility() == View.INVISIBLE) {
                            temp.setVisibility(View.VISIBLE);
                        } else {
                            temp.setVisibility(View.INVISIBLE);
                        }
                    } else if(relativeLayout.getChildAt(i) instanceof GridLayout) {
                        GridLayout temp = (GridLayout) relativeLayout.getChildAt(i);
                        for(int g = 0; g < temp.getChildCount(); g++) {
                            Button tempButton = (Button) temp.getChildAt(g);
                            if(tempButton.getVisibility() == View.INVISIBLE) {
                                tempButton.setVisibility(View.VISIBLE);
                            } else {
                                temp.setVisibility(View.INVISIBLE);
                            }
                        }
                    }
                }
            }

            return false;
        }
    });

【讨论】:

  • 谢谢,检查我上面的编辑,看看是否有任何改变。
  • 我刚试过你的代码,它抛出:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridLayout.setOnTouchListener
  • 我不确定这是否可行。我添加了对 onInterceptTouchEvent() 的覆盖...我在自己的应用程序中实现这种类型的方法是创建了一个扩展 GridLayout 的自定义 ViewGroup。在我的 CustomGridLayout.java 文件中,我能够轻松地覆盖 onInterceptTouchEvent() 和 onTouchEvent()。这样我可以确保所有的触摸事件首先由我的自定义 ViewGroup 处理。我不知道您是否可以扩展 GridLayout 来完成这项工作。
  • 我实际上只是让您的原始代码用于网格布局部分,并进行了一些调整。问题是当我尝试在包含网格布局的整个相对布局上使用该代码时,该代码不起作用。我的 XML 具有更多按钮,而不仅仅是我需要显示/隐藏的 dpad。您是否有一个等效的示例适用于相对布局中包含的所有内容? (感谢您的帮助!)
  • 酷!我想您可以尝试用 RelativeLayout 替换 GridLayout 并放入适当的视图 ID。所以`RelativeLayout allButtons = (RelativeLayout) findViewById(R.id.yourIdHere);老实说,我不确定这个技巧会有多少层深,但值得一试。我很高兴你至少可以让它发挥作用。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多