Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OnTouchListener不';不能使用相对布局_Java_Android - Fatal编程技术网

Java OnTouchListener不';不能使用相对布局

Java OnTouchListener不';不能使用相对布局,java,android,Java,Android,我有一个RelativeLayout,其中动态添加了许多textView。我面临的问题是,每当我将onTouch侦听器单独应用于TextView时,它会检测到触摸,但当我将触摸添加到相对布局时,它永远不会响应 此代码可以检测触摸事件: TextView电视=新的TextView(本); tv.setText(值[i]); 可绘制d=getResources().getDrawable(R.Drawable.level\u small\u corners); tv.setClickable(真);

我有一个
RelativeLayout
,其中动态添加了许多
textView
。我面临的问题是,每当我将onTouch侦听器单独应用于
TextView
时,它会检测到触摸,但当我将触摸添加到相对布局时,它永远不会响应

此代码可以检测触摸事件:

TextView电视=新的TextView(本);
tv.setText(值[i]);
可绘制d=getResources().getDrawable(R.Drawable.level\u small\u corners);
tv.setClickable(真);
tv.setId(i+1);
电视节目(18),;
tv.setOnTouchListener(cellTouch);
但当我在myRelativeLayout中添加所有这些文本视图时:

myRelativeLayout.setOnTouchListener(cellTouch);
现在,侦听器从未被调用。为什么会这样


.
.
.
.
.
.

您必须将您的
RelativeLayout
设置为true:
android:clickable=“true”
在myRelativeLayout.xml中添加:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
正在导致您的布局,而不是触发触摸事件。尝试删除此项。

此项对我有效:

yourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {  
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        //gesture detector to detect swipe.
        gestureDetector.onTouchEvent(arg1);
        return true;//always return true to consume event
    }
});

也许在你看来,有些东西“在”我的相对论之上。如果是,它将首先获得触摸事件。最糟糕的是,事件的默认处理方式是不处理事件,然后使用它

一种解决方案是将此代码添加到应处理事件的视图(或布局)“上方”显示的任何组件上:

someView.setOnTouchListener (new View.OnTouchListener()
{
    @Override
    public boolean onTouch (View v, MotionEvent event)
    {
        return false;
    }
});

关键(显然)是返回false。当您这样做时,事件将不会被消费,而是“向下”传递到您相对布局中的某个地方——希望是您希望它去的地方。

我知道这不是您问题的解决方案,但您的问题与我的问题最接近,因此这可能会帮助其他人

尽管在设计师看来是这样,但在某些设置下,布局实际上并不是“屏幕宽度”

宽度环绕,填充/布局中无点击事件

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dip"
            android:layout_height="fill_parent"

我已经试过了。这不起作用。看看我做的添加,它不起作用,因为android:clickable开关指的是使用clickListener,而不是touchListener。但这难道不意味着文本视图不能通过触摸来聚焦吗?为什么设置clickable disable会禁用触摸,我想这会启用它。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"