首页 新闻 会员 周边

点击事件效应奇怪

0
悬赏园豆:50 [待解决问题]

我在一个字布局文件中放置了一张图片和2个文本,子布局是绝对布局,然后用for循环动态载入10次放进一个LinearLayout中,动态布局中每个布局文件返回一个View,然后我给这个view注册了OnClickListener事件,结果是这个事件效应的很奇怪,点击图片的位置没有反应,但是在图片的间隙就触发了该事件,我在问题中负有我自己的源码,等待解决。

public class TestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        LinearLayout   container = (LinearLayout ) this.findViewById(R.id.container);
        for(int i =0;i<10;i++){
         View dView = View.inflate(this, R.layout.recomvideosh, null);
         dView.setOnClickListener(this);
         container.addView(dView);
        }
    }

 @Override
 
 
 public void onClick(View v) {
  // TODO Auto-generated method stub
  System.out.println("click me");
 }
}

黄色部分就是布局文件,其结构如下

<AbsoluteLayout android:id="@+id/cs"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_marginTop="7px" android:layout_marginBottom="10px"
 android:paddingTop="10px" android:layout_height="70px"
 android:paddingLeft="17px" android:paddingRight="23px"
 android:layout_width="157px" android:clickable="true">
 <TextView android:layout_height="wrap_content"
  android:layout_width="wrap_content" android:layout_x="3dip"
  android:layout_y="23dip" android:id="@+id/bfdz" android:visibility="invisible"></TextView>
 <ImageButton android:layout_x="0dip" android:layout_y="0dip"
  android:layout_height="60px" android:layout_width="fill_parent"
  android:id="@+id/imgShow" android:clickable="true" android:background="@drawable/slt"></ImageButton>
 <TextView android:layout_height="wrap_content"
  android:layout_x="5dip" android:layout_y="40dip" android:id="@+id/title"
  android:layout_width="115px"></TextView>
a</AbsoluteLayout>

飞翔宝鸡的主页 飞翔宝鸡 | 初学一级 | 园豆:150
提问于:2011-03-30 13:19
< >
分享
所有回答(1)
0

1、在用android布局的时候,最好不要用绝对布局(这个布局API也不推荐使用,动态代码添加的时候会显示黄色线条注意到没)

2、你的绝对布局中“两个文本,一张图片”,其实是“两个文本,一个ImageButton”,而ImageButton的意思是图片按钮,也就是图片类型的按钮。其本质还是按钮,按钮本身有焦点,你点击图片的时候其实是触发了按钮本身(也就是当前鼠标焦点在按钮上而不是外层的view)。所以它响应的是按钮事件而不是view。而你的点击是对view本身的监听。

3、如果你想点击按钮也不响应,只相应view,那么你可以在通过dView 找到对应按钮后(比如是button1,用button1.setFocusable(false);)不让该按钮取到焦点即可

4、你可以测试相应imagebutton按钮,那么你同样找到对应按钮后可以为其设置监听。如button.setOnClickListener(this); 那么你只有点击按钮才会显示:click me,点击其他地方没反应...

5、朋友,希望可以帮到你! 

awen_PC | 园豆:227 (菜鸟二级) | 2011-12-05 11:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册