当前位置:编程学习 > wap >>

LinearLayout监听问题

在Activity中的代码:
        set_reminder_layout = (LinearLayout) date_time_setting.findViewById(R.id.set_reminder_layout);
        set_circle_layout = (LinearLayout) date_time_setting.findViewById(R.id.set_circle_layout);
        set_reminder_layout.setOnTouchListener(new LayoutClickListener());
        set_circle_layout.setOnTouchListener(new LayoutClickListener());

    private class LayoutClickListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int id = v.getId();
            System.out.println("Asd");
            switch(id) {
            case R.id.set_circle_layout:
                Toast.makeText(MemoryPad.this, "set_circle_layout", Toast.LENGTH_SHORT).show();
                break;
            case R.id.set_reminder_layout:
                Toast.makeText(MemoryPad.this, "set_reminder_layout", Toast.LENGTH_SHORT).show();
                break;               
            }
            return false;
        }
    }
其中一个线性布局对象在date_time_setting.xml中的属性设置,实在不知道怎办,把属性能true的都true了
                   android:id="@+id/set_reminder_layout"
                   android:focusable="true"
                   android:focusableInTouchMode="true"
                   android:clickable="true"
                   android:orientation="horizontal"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="10dp"
                   android:layout_marginBottom="10dp"
main.xml文件中使用了include标签,引用了date_time_setting.xml,两个线性布局对象在date_time_setting.xml文件中,而且这两个线性布局是在date_time_setting.xml中另外的线性布局中,被包了几层,
程序允许的结果是,没有报错,但是程序没有按照监听事件的效果呈现出来。试了setOnClickListener,也没有效果。
我要的效果就是实现这两个布局对象的监听,产生相应的结果即可。请各位路过走过的大虾指导指导。 --------------------编程问答-------------------- 把完整的XML文件贴出来吧,看下你layout的布局层次,楼主说的这种情况可能是因为外层的layout或者其他控件接收并且消费掉了OnTouch事件 --------------------编程问答-------------------- date_time_setting.xml文件的全部内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      >
     <ImageView  
         android:background="#ffdddddd" 
         android:layout_width="match_parent" 
         android:layout_height="1.0px" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp" />
     <LinearLayout 
         android:gravity="center" 
         android:orientation="horizontal" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent">
         <ImageView 
        android:background="#ffdddddd"
        android:layout_width="1.0px"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"/>
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
               <LinearLayout  
                   android:id="@+id/set_reminder_layout"
                   android:focusable="true"
                   android:focusableInTouchMode="true"
                   android:clickable="true"
                   android:orientation="horizontal"
                   android:layout_width="match_parent" 
                   android:layout_height="wrap_content"
                   android:layout_marginTop="10dp"
                   android:layout_marginBottom="10dp">              
                   <TextView 
                       android:textSize="14.0sp" 
                       android:gravity="left|center" 
                       android:layout_gravity="center" 
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content"
                       android:text="提醒" 
                       android:drawableLeft="@drawable/ic_remind"
                       android:drawablePadding="2.0dip" 
                       android:clickable="false"
                       android:focusable="false"/>
                   <TextView 
                       android:textSize="18.0sp" 
                       android:gravity="left|center"  
                       android:paddingLeft="10.0dip" 
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content" 
                       android:text="不提醒" 
                       android:layout_weight="1.0" 
                       android:clickable="false"
                       android:focusable="false"/>
                   <TextView 
                       android:gravity="right|center" 
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content" 
                       android:layout_weight="1.0" 
                       android:drawableRight="@drawable/down_arrow"
                       android:layout_marginRight="10dp"
                       android:focusable="false"/>                
               </LinearLayout>
       <ImageView  
         android:background="#ffdddddd" 
         android:layout_width="match_parent" 
         android:layout_height="1.0px" 
         android:clickable="false" />
               <LinearLayout  
                   android:id="@+id/set_circle_layout"
                   android:focusable="true"
                   android:focusableInTouchMode="true"
                   android:clickable="true"
                   android:orientation="horizontal"
                   android:layout_width="match_parent" 
                   android:layout_height="match_parent"
                   android:layout_marginTop="10dp"
                   android:layout_marginBottom="10dp">
                   <TextView 
                       android:textSize="14.0sp" 
                       android:gravity="center" 
                       android:layout_gravity="left|center"
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content" 
                       android:text="重复" 
                       android:drawableLeft="@drawable/ic_repeat" 
                       android:drawablePadding="2dp"
                       android:clickable="false" />
                   <TextView 
                       android:textSize="18.0sp"
                       android:gravity="left|center" 
                       android:paddingLeft="10.0dp" 
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content" 
                       android:text="不重复" 
                       android:layout_weight="1.0"
                       android:clickable="false" />
                   <TextView 
                       android:gravity="right|center" 
                       android:layout_width="wrap_content" 
                       android:layout_height="wrap_content" 
                       android:layout_weight="1.0" 
                       android:drawableRight="@drawable/down_arrow"
                       android:layout_marginRight="10dp"
                       android:clickable="false"/>                    
               </LinearLayout>
   <ImageView  
          android:background="#ffdddddd" 
          android:paddingLeft="40dp" 
          android:layout_width="match_parent" 
           android:layout_height="1.0px" 
           android:clickable="false" />
           </LinearLayout> 
            <ImageView 
        android:background="#ffdddddd"
        android:layout_width="1.0px"
        android:layout_height="match_parent"
        android:layout_marginRight="20dp"
        android:clickable="false" />
     </LinearLayout> 
</LinearLayout>     --------------------编程问答-------------------- main.xml的全部内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
     android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
     android:layout_weight="1.0"
     android:scrollbarSize="12dip">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >           
    <EditText 
        android:id="@+id/et_memory_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
android:layout_margin="5dp"
        android:hint="备忘录标题"
        android:singleLine="true"
        android:maxLength="16"
        android:layout_alignParentTop="true" />     
    <EditText 
        android:id="@+id/et_memory_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
android:layout_margin="5dp"
android:maxLines="8"
android:maxLength="128"
        android:hint="备忘录内容" />           
    <Button 
        android:id="@+id/btn_setDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="日期设置"
        android:gravity="left|center"
        android:layout_margin="5dp"
        android:hint="提醒"
        android:drawableLeft="@drawable/ic_remind"/>
    <Button 
        android:id="@+id/btn_setCircle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="不重复"
        android:hint="重复"
        android:gravity="left|center"
        android:layout_margin="5dp"
        android:drawableLeft="@drawable/ic_repeat"/>
    </LinearLayout>
    <include layout="@layout/date_time_setting"/>                                                                                                                                                                                                                                                                                       
</LinearLayout>
</ScrollView>
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8.0"
    android:layout_alignParentBottom="true">
    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0" 
        android:text="保存"
        android:layout_margin="5dp"
        android:layout_alignParentBottom="true"/>
    <Button 
        android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:layout_margin="5dp"
        android:text="取消"
        android:layout_alignParentBottom="true"/>
</LinearLayout>
</LinearLayout> --------------------编程问答--------------------
引用 1 楼 lingang1359 的回复:
把完整的XML文件贴出来吧,看下你layout的布局层次,楼主说的这种情况可能是因为外层的layout或者其他控件接收并且消费掉了OnTouch事件
我把两个xml文件的全部内容贴出来了 --------------------编程问答-------------------- 你外层套了个ScrollView,ScrollView会拦截并消费掉你的OnTouch事件,得自己重写OnTouch事件,判断滑动的时候调用ScrollView的OnTouch事件,点击的时候就不要拦截,继续把该事件往下传。 --------------------编程问答-------------------- 我按我理解你的意思添加了这几行代码,结果就是我这里没响应,所以你的话能我没理解透,可以请你讲得再明白一点吗?
ScrollView sv;// 成员变量
sv = (ScrollView)this.findViewById(R.id.scrollView);
sv.setOnTouchListener(new ScrollViewOnTouchListener());
private class ScrollViewOnTouchListener implements OnTouchListener {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) return true;
if (event.getAction() == MotionEvent.ACTION_DOWN) return false;
return false;
}
}
--------------------编程问答--------------------
引用 5 楼 lingang1359 的回复:
你外层套了个ScrollView,ScrollView会拦截并消费掉你的OnTouch事件,得自己重写OnTouch事件,判断滑动的时候调用ScrollView的OnTouch事件,点击的时候就不要拦截,继续把该事件往下传。

我按我理解你的意思添加了这几行代码,结果就是我这里没响应,所以你的话能我没理解透,可以请你讲得再明白一点吗?
ScrollView sv;// 成员变量
sv = (ScrollView)this.findViewById(R.id.scrollView);
sv.setOnTouchListener(new ScrollViewOnTouchListener());
private class ScrollViewOnTouchListener implements OnTouchListener {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) return true;
if (event.getAction() == MotionEvent.ACTION_DOWN) return false;
return false;
}
} --------------------编程问答-------------------- 我先去完成作业的其他部分功能,这个问题自己再慢慢地想想吧。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,