开发者

How do I find a non child View in Android?

I have a TextView inside the same RelativeLa开发者_StackOverflowyout as a chunk of ImageView derived custom classes. I'd like to be able to write to the TextView from inside these other Views. How do I do this since I am unable to use findViewById due to it not being a child?


I'd like to be able to write to the TextView from inside these other Views.

That is not a good design. Views should not interact with other random Views. Communication between those should be handled by the Activity, or possibly by making the RelativeLayout be a custom View and having it mediate the communication.


You can use findViewById of current Activity to find those other Views.

public class CustomView extends View {
    private Activity activity;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (Activity) context;
    }

    private View findOtherView(int id) {
        return activity.findViewById(id);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜