Can I assign a “default” OnClickListener() for an Android Activity?

余生长醉 提交于 2019-12-06 01:21:47

Not to my knowledge, but you could just use a loop, something like this:

ViewGroup root = findViewById(R.id.my_root_layout);
final Context context = this;
assignClickHandler(root);

public void assignClickHandler(int root) {
    for(int i = 0; i < root.getChildCount(); i++) {
        if(root.getChildAt(i) instanceof ViewGroup) {
            assignClickHandler(root.getChildAt(i));
        }
        else {
            (root.getChildAt(i)).setOnClickListener(context);
        }
    }
}

Note it calls recursively for any nested layouts within as well. I haven't tested this so I might have messed up some syntax, but that idea should work, if you're just looking to avoid manually setting every one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!