如何使用字符串作为Android的一个的ResourceID [英] How to Use String as a ResourceID in Android

查看:114
本文介绍了如何使用字符串作为Android的一个的ResourceID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个的TextView s或其他组件,无所谓。而意见有重复的ID,如: textView1,textView2,textView3

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.

只要我想用的 pre-字符串值来迭代IDS。

Simply I want to iterate ids by using pre-string values.

的伪例如:

String pre_value = "textView";

for(int i = 0; i < size; i++) {

    String usable_resource_id = pre_value + Integer.toString(pre_value);
    // So how to use these id like R.id.textView1
    // Cast or something similar

}

有什么建议?

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29"相对=nofollow> 资源#则getIdentifier() ,以得到一个字符串的标识符。

You can use Resources#getIdentifier() to get the identifier from a string.

但是,如果你要遍历他们,那岂不是更容易保持的id在数组或列表?

But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

这篇关于如何使用字符串作为Android的一个的ResourceID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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