如何添加Edittexts或布局块在我的活动需求? [英] How can I add Edittexts or Layout-blocks on demand in my Activity?
问题描述
这是一个有点棘手解释,这可能是为什么我还没有发现在网络上的任何解决方案的原因至今...
我有一个包含两个变量的类 - 一个String和字符串的LinkedList的:
字符串名称;
LinkedList的<串GT; StringList的;
通常StringList的弦乐1-4之间成立。现在,我想创建这说明对象的内容从这个类,显示的EditText字段动态,取决于值的LinkedList的量的活动。
我想象它看起来像这样当有列表中的4个值:
标题:LT;名称>
=============
值:或其可StringList的[0]≥
值:或其可StringList的[1]≥
值:或其可StringList的[2]≥
值:或其可StringList的[3]≥
...和像这样,如果只有一个值:
标题:LT;名称>
=============
值:或其可StringList的[0]≥
如何定义我的布局XML的文件才能够做到这一点?在我layoutfile,在我的code难道我宣布我的TextView,EditText上,结合只有一次再版,直到它匹配stringlist.length()?我能以某种方式做到这一点的重复也为整个布局块?
//尝试这种方式,希望这将帮助你解决你的问题。
my.xml
<的EditText
机器人:ID =@ + ID / edtTitle
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
机器人:提示=标题/> <的LinearLayout
机器人:ID =@ + ID / lnrDynamicString
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
机器人:layout_marginTop =5DP
机器人:方向=垂直> < / LinearLayout中>< / LinearLayout中>
MyActivity
公共类MyActivity延伸活动{ 我的我的;
私人的EditText edtTitle;
私人的LinearLayout lnrDynamicString;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.my); edtTitle =(EditText上)findViewById(R.id.edtTitle);
lnrDynamicString =(的LinearLayout)findViewById(R.id.lnrDynamicString); 我=新我();
my.setTitle(MyTitle);
LinkedList的<串GT; tempString =新的LinkedList<串GT;();
tempString.add(Child1);
tempString.add(CHILD2);
tempString.add(Child3);
tempString.add(Child4);
tempString.add(Child5);
my.setStringlist(tempString); edtTitle.setText(my.getTitle());
的for(int i = 0; I< my.getStringlist()大小();我++){
的EditText edtChild =新的EditText(本);
edtChild.setLayoutParams(新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
edtChild.setId(ⅰ);
edtChild.setText(my.getStringlist()获得(一));
lnrDynamicString.addView(edtChild);
}
}}** **我
公共类我{
公共字符串的getTitle(){
返回称号;
} 公共无效的setTitle(字符串名称){
this.title =称号;
} 私人字符串称号; 公众的LinkedList<串GT; getStringlist(){
返回StringList的;
} 公共无效setStringlist(LinkedList的<串GT;的StringList){
this.stringlist = StringList的;
} 私人的LinkedList<串GT; StringList的;
}
This is a bit tricky to explain, that's probably the reason why I haven't found any solution on the web so far...
I have a class which contains two variables - a String and a LinkedList of Strings:
String name;
LinkedList<String> stringlist;
stringlist normally holds between 1-4 Strings. Now I would like to create an Activity which shows the content of the objects from this class and shows EditText-fields dynamically, depending on the amount of values in the LinkedList.
I imagine it to look like this when there are 4 values in the list:
Title: <name>
=============
Value: <stringlist[0]>
Value: <stringlist[1]>
Value: <stringlist[2]>
Value: <stringlist[3]>
... and like this if there is only one value:
Title: <name>
=============
Value: <stringlist[0]>
How do I define my layout xml-file to be able to do this? Do I declare my "TextView-EditText-Combination" only once in my layoutfile and in my code to reprint this until it matches stringlist.length() ? Can I somehow do this repetition also for a whole layout-block?
// try this way hope this will help you solve your problem.
my.xml
<EditText
android:id="@+id/edtTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Title"/>
<LinearLayout
android:id="@+id/lnrDynamicString"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
MyActivity
public class MyActivity extends Activity {
My my;
private EditText edtTitle;
private LinearLayout lnrDynamicString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my);
edtTitle =(EditText) findViewById(R.id.edtTitle);
lnrDynamicString=(LinearLayout)findViewById(R.id.lnrDynamicString);
my = new My();
my.setTitle("MyTitle");
LinkedList<String> tempString = new LinkedList<String>();
tempString.add("Child1");
tempString.add("Child2");
tempString.add("Child3");
tempString.add("Child4");
tempString.add("Child5");
my.setStringlist(tempString);
edtTitle.setText(my.getTitle());
for (int i=0;i<my.getStringlist().size();i++){
EditText edtChild = new EditText(this);
edtChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
edtChild.setId(i);
edtChild.setText(my.getStringlist().get(i));
lnrDynamicString.addView(edtChild);
}
}
}
**My**
public class My {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private String title;
public LinkedList<String> getStringlist() {
return stringlist;
}
public void setStringlist(LinkedList<String> stringlist) {
this.stringlist = stringlist;
}
private LinkedList<String> stringlist;
}
这篇关于如何添加Edittexts或布局块在我的活动需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!