查找视图的名字 [英] Find view by name

查看:394
本文介绍了查找视图的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能找到一个鉴于它的名字,而不是它的id。

  findViewById(R.id.someView);
 

但我希望做这样的事情:

  findViewByName(someView);
 

解决方案

您在处理XML处理发现的观点被识别,但你可以看一下标识使用<一个href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29">getIdentifier(String名,...)如果你有你的布局编号,例如这是有用的。要知道,这样的查询是比较昂贵的。

完成答案

 内部ID = getResources()则getIdentifier(姓名,ID,context.getPackageName())。
查看查看= findViewById(ID);
 

Is it possible to find a view by its name rather than its id.

findViewById(R.id.someView);

but I would like to do something like this:

findViewByName("someView");

解决方案

you have to find views by identifier when dealing with xml, but you can look up the identifier by using getIdentifier(String name, ...) which is useful if you have your layouts numbered for example. Just be aware that such a lookup is relatively expensive.

to complete the answer

int id = getResources().getIdentifier(name, "id", context.getPackageName());
View view = findViewById(id);

这篇关于查找视图的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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