【问题标题】:Fragment using ScrollView inside RelativeLayout > ontouch doesn't work在 RelativeLayout > ontouch 中使用 ScrollView 的片段不起作用
【发布时间】:2012-11-09 23:19:25
【问题描述】:

几个小时以来,我一直在努力寻找解决方案,感觉就像我搜索了 stackoverflow 上的每个问题并尝试了布局中的所有内容。 在我尝试下面显示的布局之前,edittext 和两个图像按钮位于滚动视图内。在这个旧布局中,ontouch 在我的片段中完美运行,但是一旦我使用edittext 和两个图像按钮拉出相对布局,ontouch 就没有被触发。有人可以帮我解决这个问题吗?任何帮助表示赞赏。谢谢

View fragmentView = inflater.inflate(R.layout.request, container, false);

fragmentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

            }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="#BDCCE0" >

    <EditText
        android:id="@+id/etsearch"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="10dp"
        android:hint="search..."
        android:singleLine="true"
        android:imeOptions="actionDone" />

    <ImageButton
        android:id="@+id/bsearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/etsearch"
        android:layout_marginTop="9dp"
        android:src="@drawable/search" />

    <ImageButton
        android:id="@+id/bupdaterequests"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/bsearch"
        android:layout_marginTop="9dp"
        android:src="@drawable/refresh" />

  <ScrollView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:background="#BDCCE0"
        android:layout_below="@id/etsearch" >

   <TableLayout
       android:id="@+id/myTableLayout"  
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content" >    

   </TableLayout> 
 </ScrollView>
</RelativeLayout>

【问题讨论】:

    标签: android scrollview android-relativelayout touch-event


    【解决方案1】:

    TouchEvent 从“最高”元素开始,并“向下”渗透到 View 层次结构中,直到它被某个事件消耗。在这种情况下,ScrollView、Buttons 或 EditText 都会在事件到达 RelativeLayout 之前消耗掉事件...


    基本上我只想当有人在编辑文本之外触摸时键盘消失。

    我通过简单地制作根布局clickablefocusable 来解决这个问题。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    

    【讨论】:

    • 你想让RelativeLayout的OnTouchListener做什么?会不会和其他View的基本功能冲突?
    • 基本上我只想当有人在编辑文本之外触摸时键盘消失。我的代码通常适用于 ontouch,但由于 ontouch 不会触发,我不知道如何解决这个问题。
    • 我确实在包含按钮的表格布局中添加了一些内容,因此我无法覆盖整个表格布局。
    • 我工作,但只有当我点击编辑文本上方时。由于我们已经非常接近解决问题,因此我会投票赞成您的回答。
    • 如果你也制作 ScrollView focusable 会怎样?
    【解决方案2】:

    android:clickable="true" 解决了这个问题..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2014-06-14
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多