I'm a novice in the java world, so thanks in advance for the help.
我是java世界的新手,所以提前感谢你的帮助。
What I am trying to do is this: I have two EditText fields and a Button. When I hit the button, I want to take the contents of the two fields, combine them and display them below button. The fields are cleared. Then if they are filled again and the button is pushed, combine the fields and display them below previously generated textview.
我想要做的是:我有两个EditText字段和一个Button。当我按下按钮时,我想获取两个字段的内容,组合它们并在按钮下方显示它们。字段已清除。然后,如果再次填充按钮并按下按钮,则组合字段并在先前生成的textview下方显示它们。
Example:
Before Button Click:
eT1___ eT2____ |Button|
eT1___ eT2____ |按钮|
After Button click
eT1___ eT2____ |Button|
eT1-eT2
eT1___ eT2____ |按钮| ET1,ET2
After 2nd Button click
eT1___ eT2____ |Button|
eT1-eT2
eT1-eT2
eT1___ eT2____ |按钮| eT1-eT2 eT1-eT2
And so on however many times the button is clicked. What I need help with is the new views that are being added. Currently I am using a Relative Layout. What I am trying to figure out is how to reference the new textView's that I am creating within the code so that when new views are added, they can reference the previously added view.
等等按钮被点击了很多次。我需要帮助的是正在添加的新视图。目前我正在使用相对布局。我想弄清楚的是如何引用我在代码中创建的新textView,以便在添加新视图时,它们可以引用先前添加的视图。
This is what I've been attempting at the moment:
这就是我此刻一直在尝试的事情:
private TextView createNewTextView(String desc, Double cost){
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);;
TextView newTextView = new TextView(this);
if (i==1) {
lparams.addRule(RelativeLayout.BELOW, mButton.getId());
}
else {
lparams.addRule(RelativeLayout.BELOW, mLayout.getId());
}
lparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newTextView.setLayoutParams(lparams);
newTextView.setText(desc + " - $" + cost);
newTextView.setTag(i);
i++;
return newTextView;
}
I was trying to use "i" as an incrementing variable that I could use to reference the previous view by except on the first pass when it the new view references based on the button.
我试图使用“i”作为递增变量,我可以使用它来引用前一个视图,除了在新视图基于按钮引用时的第一个传递。
Any help would be greatly appreciated. Thanks in advance and for everything I've lurked already!
任何帮助将不胜感激。在此先感谢我已经潜伏的一切!
2 个解决方案
#1
0
Create a member variable of ArrayList
of TextView
:
创建TextView的ArrayList的成员变量:
ArrayList<TextView> textviews = new ArrayList<>();
and then in your createNewTextView()
method, store the reference of TextView
you are adding:
然后在createNewTextView()方法中,存储要添加的TextView的引用:
textviews.add(newTextView);
So the TextView
you've added at the first will be at index 0 and so on. Then you can easily reference your TextViews
.
因此,您在第一个添加的TextView将位于索引0,依此类推。然后,您可以轻松引用TextView。
Suppose you want to get the second TextView
,
假设您想获得第二个TextView,
TextView secondTv = textviews.get(1);
#2
0
Just add your textviews to a global list and find them based on the tag from there.
只需将您的文本视图添加到全局列表中,然后根据该标记找到它们。
#1
0
Create a member variable of ArrayList
of TextView
:
创建TextView的ArrayList的成员变量:
ArrayList<TextView> textviews = new ArrayList<>();
and then in your createNewTextView()
method, store the reference of TextView
you are adding:
然后在createNewTextView()方法中,存储要添加的TextView的引用:
textviews.add(newTextView);
So the TextView
you've added at the first will be at index 0 and so on. Then you can easily reference your TextViews
.
因此,您在第一个添加的TextView将位于索引0,依此类推。然后,您可以轻松引用TextView。
Suppose you want to get the second TextView
,
假设您想获得第二个TextView,
TextView secondTv = textviews.get(1);
#2
0
Just add your textviews to a global list and find them based on the tag from there.
只需将您的文本视图添加到全局列表中,然后根据该标记找到它们。