从字符串获取android的id资源 [英] Get android's id resource from a string

查看:216
本文介绍了从字符串获取android的id资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Kotlin应用程序中,我有一些ImageViews(在 activity_main.xml 中):imageView_0imageView_1imageView_2imageView_3.

In my Kotlin app I have some ImageViews (in activity_main.xml): imageView_0, imageView_1, imageView_2 and imageView_3.

如何从0到3循环访问视图?这是行不通的:

How can I access the view in a loop from 0 to 3? This won't work:

val imageView: ImageView = findViewById<ImageView>("R.id.imageView_" + index) as ImageView

推荐答案

for(i in 1..3){
  val id: int=getResources().getIdentifier("imageview_"+i, "id", 
  getPackageName())
  imageview[i]=findViewById(id) as ImageView
}

如果您在 xml imageview_1imageview_2imageview_3

这篇关于从字符串获取android的id资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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