android - 为View添加onClicklistener没有生效

查看:513
本文介绍了android - 为View添加onClicklistener没有生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

问题是这样的,我说下我的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!!!");
        }
    });
    
    

求大神帮忙,跪谢

解决方案

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

这篇关于android - 为View添加onClicklistener没有生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆