开发者

is there something equaivlent getChildViewByID in android

I want to get a a childView whose name is "name" inside a ViewGroup. I knew that开发者_运维知识库 the nameView is the second item inside the GroupView, so I used following code:

TextView nameView =  (TextView)((ViewGroup)view).getChildAt(2); //2 is magic number

However, it is possible that ,in the future, I might add some other Views before the nameView and above code will became invalid. So, I am wondering how could get the ChildView by name, something like:

     TextView nameView =  (TextView)((ViewGroup)view).getChildByID(R.id.name); 

EDIT: thanks to kcoppock. I realize getChildByName("name") does not make sense. So i will purse something like getChildByID.


You method are looking for is View.findViewById(id). Per your example:

TextView nameView =  (TextView) view.findViewByID(R.id.name);

(It is not necessary to cast to ViewGroup)

Barry


If you just need to find a particular child of a ViewGroup by its ID, just first get a reference to the ViewGroup:

ViewGroup vg = (ViewGroup)findViewById(R.id.myGroupLayout);

Then call findViewById() on that ViewGroup:

View v = (TextView)vg.findViewById(R.id.name);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜