【问题标题】:Create a TextView with an ID from a String Array - Android使用字符串数组中的 ID 创建 TextView - Android
【发布时间】:2013-02-18 06:54:04
【问题描述】:

在 Android 中,我想以编程方式创建多个具有各自 ID 的 TextView,以便我可以使用例如 FOR 语句创建一定数量的具有自己 ID 的 TextView。

例如,

// Created TextView prior

for(int i=0;i<10;i++) {
    linearlayout.addView(ID);
}

所以我想知道我是否有可能拥有一个具有几个不同 ID 的字符串数组,并以某种方式通过字符串数组的位置为每个 TextView 提供其 ID。因此,例如,当字符串数组为“frogs”时,TextView 的 ID 为“frogs”,然后随着 FOR 语句的增加,定义 TextView ID 的字符串数组的值也会增加(如果它甚至可能在全部)。

谢谢, 布伦丹

【问题讨论】:

    标签: java android arrays textview


    【解决方案1】:

    所以我想知道我是否有可能拥有一个具有几个不同 ID 的字符串数组,并以某种方式通过字符串数组的位置为每个 TextView 提供其 ID

    是的,你可以拥有它:

    String[] ids = new String[] {"1","2","3","4","5"};
    for(int item = 0; item < ids.length; item++) 
     {
           TextView textView = new TextView(this);
           // careful id value should be a positive number.
           textView.setText(ids[item]);
           textView.setId(parseInt(ids[item]));
           ...
      }
    

    【讨论】:

    • 哦,是的,我明白了,尽管我不得不使用 textview.setId(item) 因为 parseInt 不起作用。不过非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2013-06-13
    相关资源
    最近更新 更多