android - 为View添加onClicklistener没有生效
ringa_lee
ringa_lee 2017-04-17 17:28:44
0
3
611

问题是这样的,我说下我的app大致流程:
1、自定义1个layout;
2、activity inflate这个layout
3、为这个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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground"
    tools:ignore="UselessParent">

    <TextView
        android:id="@android:id/text1"
        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() {
        @Override
        public void onClick(View v) {
            MyLod.log("in activity,cao on click!!!");
        }
    });
    
    

求大神帮忙,跪谢

ringa_lee
ringa_lee

ringa_lee

reply all(3)
迷茫

vvv = findViewById(R.id.sss); This sentence should be found through the view that is inflated. If mView is inflated, the code should be vvv = mView.findViewById(R.id.sss); and then set the listener. The above is my opinion

洪涛

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

Peter_Zhu

Can’t see which id of sss is displayed

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template