简体   繁体   English

Android TextView / EditText动态生成

[英]Android TextView/EditText generated dynamically

In my app i generate TextViews/Edits dynamically during the execution. 在我的应用程序中,我在执行过程中动态生成TextViews / Edits。 I also have a TableLayout so each set of TextEdit/TextViews will be on a new row. 我也有一个TableLayout,所以每组TextEdit / TextViews都会在新行上。 I want to use the row number (1...N) as a sort of ID to identify each set of textEdit/TextViews (Example, row 1 will have TEdit1, Tview1, row 2 will have TEdit2, TView2..etc). 我想使用行号(1 ... N)作为一种ID来标识每组textEdit / TextViews(例如,第1行将具有TEdit1,Tview1,第2行将具有TEdit2,TView2..etc)。

I have functions that are called on the onchange of each tEdit. 我有在每个tEdit的onchange上调用的函数。 For instance, TEdit 1 changes the value of TView1 when the onChange method is called. 例如,当调用onChange方法时,TEdit 1会更改TView1的值。 The function is set up so that i can pass the TView id and it changes it properly. 设置该功能,以便我可以传递TView ID并正确更改它。 My problem is, i dont know how to use a variable as the tView id. 我的问题是,我不知道如何使用变量作为tView ID。 The function takes for parameter, among others, the rowId (1, 2, 3..etc) so i want to know if there's a way to set up a variable like this: 该函数接受参数,其中包括rowId(1、2、3..etc),因此我想知道是否有办法设置这样的变量:

String VariableName = "tEdit" + i; (i is the rowId that its passed through as a parameter)

And then use it for something like this: 然后将其用于这样的事情:

MyActivity.VariableName.setText("Change proper text");

Obviously, that one doesnt work. 显然,那是行不通的。 I have done some research and i've seen something like getIdentifier, but i've had no luck implementing that one. 我进行了一些研究,并且看到过类似getIdentifier的内容,但是我没有实现该功能的运气。 Any help would be greatly appreciated. 任何帮助将不胜感激。

If I have well understood your question maybe you can find useful "Tag" property. 如果我很了解您的问题,也许您会找到有用的“标记”属性。 I mean that when you dynamically add your new row to the table you can simply add a tag identifier to this new row with his index position inside the table 我的意思是,当您将新行动态添加到表中时,您可以简单地向该新行添加标签标识符,并将其索引位置放在表中

TableRow tr = new TableRow(this);
tr.setTag(indexPosition);

So, when you need to know the position of the current row you'll simply need to call 因此,当您需要知道当前行的位置时,只需调用

int position = tr.getTag();

I hope that this was your question! 希望这是您的问题!

您可以使用Textview的数组,而不是尝试根据行号等定义自定义名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM