【问题标题】:How to set each char of a string to a different TextView in a loop如何在循环中将字符串的每个字符设置为不同的 TextView
【发布时间】:2016-05-29 12:10:51
【问题描述】:

我已经声明了所有对象,并阅读了一些关于 SO 的主题。之后我尝试设置一个数组但没有任何成功。此外,我担心的是,当我将所有 TextViews 放在一个数组中时,它们不再适用于应用程序的整个设计。

我认为我的最后一次尝试是最接近解决方案的!

示例(但没有输入 imgBeer 5 次):

if(calc.getBeerCount()<100000){
    String convert = Integer.toString(calc.getBeerCount());
    for(int i = 0; i < convert.lenght(); i++){ //edited
        imgBeer1.setText(convert.charAt(i));
        imgBeer2.setText(convert.charAt(i));
        imgBeer3.setText(convert.charAt(i));
        imgBeer4.setText(convert.charAt(i));
        imgBeer5.setText(convert.charAt(i));
    }

}else{
    txtAlcLvl.setText("For sure! Do you coun't annually?");
}

这就是我到目前为止所尝试的:

if(calc.getBeerCount()<100000){
    String convert = Integer.toString(calc.getBeerCount());
    for(int i = 0; i < convert.lenght(); i++){ //edited
        String test = "imgBeer" + i;
        test.setText(convert.charAt(i));
    }

}else{
    txtAlcLvl.setText("For sure! Do you coun't annually?");
}

当然这不起作用,但我不知道应该怎么做。数组应该是怎样的?

我也试过这个,'findViewById' 标记为红色。 'can't resolve method 'findViewById(int)'' 是显示的注释:

if(calc.getBeerCount()<100000){
    String convert = Integer.toString(calc.getBeerCount());
    for(int i = 0; i < convert.lenght(); i++){ //edited
        ((TextView)findViewById(textViewIds[i])).setText(convert.charAt(i));

    }

}else{
    txtAlcLvl.setText("For sure! Do you coun't annually?");
}

【问题讨论】:

  • 那么您可能会循环多达 100,000 次吗?你有 100,000 个文本视图吗?!?你只需要循环convert.length()
  • @Mark Kenn,你说得对,那不是我想做的。我更改了部分并用 //edited 标记了它我只想将 getBeerCount() 中的所有 5 个可能的 int 字符放在单独的 TextViews 中
  • 如果您想使用这样的动态 TextView 名称,您需要查看 Context.getResources().getIdentifier()。此外,如果您计划动态添加这些文本视图并将它们放在滚动视图中(我可以想象有很多 TextView 的唯一方法),您最好查看 RecyclerView 和适配器。
  • 我已经在xml中设置了所有的TextViews,我现在不能改变它。我有点困惑,难道不是只有一种方法可以用升序来处理几个 TextViews 吗? imgBeer + i 或类似的东西?

标签: java android for-loop char textview


【解决方案1】:

我终于弄明白了 =)

在片段类中,我实例化了一个 ArrayList 和设置 TextView 文本的方法:

private ArrayList<TextView> test = new ArrayList<>();
public void showConsumption(){

        if(calc.getBeerCount() < 100000) {
            String convert = Integer.toString(calc.getBeerCount());
            for (int i = 0; i < convert.length(); i++) {
                test.get(i).setText(Character.toString(convert.charAt(i)));
            }
        }else{
            txtAlcLvl.setText("For sure! Do you coun't annually?");
        }
    }
}

在 Fragment 的 onCreate 方法中,我将其放入:

TextView imgBeer1 = (TextView) v.findViewById(R.id.imgBeer1);
TextView imgBeer2 = (TextView) v.findViewById(R.id.imgBeer2);
TextView imgBeer3 = (TextView) v.findViewById(R.id.imgBeer3);
TextView imgBeer4 = (TextView) v.findViewById(R.id.imgBeer4);
TextView imgBeer5 = (TextView) v.findViewById(R.id.imgBeer5);

test.add(imgBeer1);
test.add(imgBeer2);
test.add(imgBeer3);
test.add(imgBeer4);
test.add(imgBeer5);

无论如何,感谢您的帮助,显然我的问题没有被问到足够的问题。

【讨论】:

    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多