现在的位置: 首页 > 综合 > 正文

Android中ListView实现子控件点击事件后ListView点击事件失效解决办法

2013年02月14日 ⁄ 综合 ⁄ 共 3130字 ⁄ 字号 评论关闭

http://blog.csdn.net/lxzh12345/article/details/9526483

最近有人问我,在Android里:

在自定义listView里面有按钮,然后setItemClickListener时不响应,网上很多关于这个的,但我都试过,貌似都解决不了。
其实刚开始学Android接触ListView时我也遇到此问题,在网上到处搜也难以找到相关解决方案,原因之一是网上原创文章少,转载居多,很多文章都雷同;原因之二在于自己英语不好,不敢上外文论坛去找。
后来经过自己的慢慢摸索终于解决了这个问题。
当朋友问我时,我一番简单讲解加几张代码截图就给他解决问题啦~
废话不多说,先上源代码:源代码
代码片段:
main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/background" >  
  6.   
  7.     <ListView  
  8.         android:id="@+id/lv_list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/numxback"  
  13.         android:cacheColorHint="#00000000"  
  14.         android:drawSelectorOnTop="false"  
  15.         android:fadingEdge="none"  
  16.         android:fastScrollEnabled="true"  
  17.         android:focusable="false"  
  18.         android:divider="@null"  
  19.         android:focusableInTouchMode="true"  
  20.         android:listSelector="@drawable/itemselected" />  
  21.   
  22. </RelativeLayout>  
在main布局里加入了一个列表控件,ListView,它的各个属性在此处不是重点,这里就不多说,看不懂的读者自己去网上搜吧!
datalist.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/widget_datalist"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:descendantFocusability="blocksDescendants">  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tv_num"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="60px"  
  12.         android:layout_centerVertical="true"  
  13.         android:background="@drawable/number"  
  14.         android:gravity="center"  
  15.         android:paddingLeft="5px"  
  16.         android:paddingRight="8px"  
  17.         android:textColor="@android:color/white"  
  18.         android:textSize="24sp" />  
  19.   
  20.     <ImageButton  
  21.         android:id="@+id/ib_edit"  
  22.         android:layout_width="60px"  
  23.         android:layout_height="60px"  
  24.         android:layout_alignParentRight="true"  
  25.         android:background="@drawable/edit"  
  26.         <span style="color:#ff0000">android:onClick="OnItemEditClick"  
  27. </span>        android:paddingRight="5px" />  
  28.   
  29.     <TextView  
  30.         android:id="@+id/tv_numx"  
  31.         android:layout_width="150px"  
  32.         android:layout_height="60px"  
  33.         android:layout_toRightOf="@+id/tv_num"  
  34.         android:background="@drawable/number"  
  35.         android:gravity="center_vertical"  
  36.         android:singleLine="true"  
  37.         android:textColor="@android:color/white"  
  38.         android:textSize="24sp" />  
  39.   
  40.     <TextView  
  41.         android:id="@+id/tv_numy"  
  42.         android:layout_width="150px"  
  43.         android:layout_height="60px"  
  44.         android:layout_toRightOf="@id/tv_numx"  
  45.         android:layout_toLeftOf="@id/ib_edit"  
  46.         android:background="@drawable/number"  
  47.         android:gravity="center_vertical"  
  48.         android:singleLine="true"  
  49.         android:textColor="@android:color/white"  

抱歉!评论已关闭.