首页 > 解决方案 > 将数据动态添加到更多 TextView

问题描述

是否可以将文本动态添加到 xalm 中定义的多个文本视图?例如,在一个循环中:

var textView1 = FindViewById<TextView>(Resource.Id.textView1);
var textView2 = FindViewById<TextView>(Resource.Id.textView2);
var textView3 = FindViewById<TextView>(Resource.Id.textView3);

var content = "Add me to more at one textView"

for (i = 1; i = 3; i++)
{
// add me;
}

我可以很容易地做到这一点,因为它通过在布局中调用 GetChildAt () 以编程方式添加 textview。如果 textView 在 xalm 中定义,是否可以添加动态?

标签: androidxamarintextview

解决方案


假设您的文本 ID 是 text1、text2、tex3 等。使用:

for (int i=1; i<numberOfTextsViews; i++) {
   int id = this.getResources().getIdentifier("text" + i, "id", getPackageName());
   TextView temp = findViewById(id);
   temp.setText("text you want to add to all your texts");
}

推荐阅读