Android: How to propagate click event to LinearLayout childs and change their drawable

后端 未结 5 1734
灰色年华
灰色年华 2021-01-31 10:05

I would like to create a linear layout which would behave similarly to ImageButton.



        
5条回答
  •  离开以前
    2021-01-31 10:35

    After having the same problem some months later, I found this solution:

    private void setOnClickListeners() {
        super.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                onClick(v);
            }
        });
        for (int index = 0; index < super.getChildCount(); index++) {
            View view = super.getChildAt(index);
            view.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    onClick(v);
                }
            });
        }
    }
    
    protected void onClick(View v) {
        // something to do here...
    }
    

提交回复
热议问题