android - 为View添加onClicklistener没有生效

【字号: 日期:2022-12-11浏览:24作者:雯心

问题描述

问题是这样的,我说下我的app大致流程:1、自定义1个layout;2、activity inflate这个layout3、为这个layout addView一个view;4、为这个view添加onClickLister;马蛋,没有生效!!!这是为什么呢??难道手动addView进去的childView点击事件无效??我在xml里面静态的包含这个View,click也是不行。。。

我这个view的xml是这样的:<FrameLayout

xmlns:android='http://schemas.android.com/apk/res/android'android:layout_width='match_parent'android:layout_height='96dp'android:layout_marginTop='0dp'android:background='#ce93d8'android:clickable='true'xmlns:tools='http://schemas.android.com/tools'><FrameLayout android: android:layout_width='match_parent' android:layout_height='match_parent' android:clickable='true' android:foreground='?attr/selectableItemBackground' tools:ignore='UselessParent'> <TextViewandroid: android:layout_width='match_parent'android:layout_height='match_parent'android:layout_gravity='top|left'android:layout_marginLeft='40dp'android:layout_marginRight='40dp'android:gravity='center'/>

</FrameLayout>

</FrameLayout>

我的静态代码是这样的:

vvv = findViewById(R.id.sss); vvv.setClickable(true); vvv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) { MyLod.log('in activity,cao on click!!!');} });

求大神帮忙,跪谢

问题解答

回答1:

vvv = findViewById(R.id.sss); 这句,应该要通过inflate过来的那个view来find。假如inflate过来的是mView 代码应该为 vvv = mView.findViewById(R.id.sss); 而后在设置监听。以上是我的看法

回答2:

R.id.sss哪个的id,还有你可以先添加setOnClickListener事件再addView();

回答3:

看不到sss的哪个id显示

相关文章: