Java 在动态编辑文本和文本视图中设置id
我在Android Studio中尝试将id放入动态edittext和texview时遇到一些问题,我想使用此id在其他函数中获取id。 注意:我没有在onCreate函数中设置任何id 这是我的代码:Java 在动态编辑文本和文本视图中设置id,java,android,android-edittext,textview,Java,Android,Android Edittext,Textview,我在Android Studio中尝试将id放入动态edittext和texview时遇到一些问题,我想使用此id在其他函数中获取id。 注意:我没有在onCreate函数中设置任何id 这是我的代码: for (Map.Entry<String,String> entry : getMap(newContact).entrySet()) { total++; TextView ProgrammaticallyT
for (Map.Entry<String,String> entry : getMap(newContact).entrySet()) {
total++;
TextView ProgrammaticallyTextView = new TextView(this.getActivity());
EditText ProgrammaticallyEditText = new EditText(this.getActivity());
ProgrammaticallyTextView.setId(total);
ProgrammaticallyEditText.setId(total+1);
ProgrammaticallyTextView.setText(entry.getKey());
ProgrammaticallyEditText.setText(entry.getValue());
linearLayout.addView(ProgrammaticallyTextView);
linearLayout.addView(ProgrammaticallyEditText);
total++;
}
for(Map.Entry:getMap(newContact.entrySet()){
总计++;
TextView ProgrammaticallyTextView=新的TextView(this.getActivity());
EditText ProgrammaticallyEditText=新的EditText(this.getActivity());
ProgrammaticallyTextView.setId(总计);
ProgrammaticallyEditText.setId(总计+1);
ProgrammaticallyTextView.setText(entry.getKey());
ProgrammaticallyEditText.setText(entry.getValue());
linearLayout.addView(ProgrammaticallyTextView);
linearLayout.addView(ProgramMaticalYeditText);
总计++;
}
这是我使用edittext和textview id的函数
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_create:
String test = "";
for(int i=0; i < (this.total); i+=2){
TextView tv = (TextView) v.findViewById(i++);
EditText et = (EditText) v.findViewById(i+2);
if ((i+2) >= this.total){
test += tv.getText()+"="+et.getText();
}else
{
test += tv.getText()+"="+et.getText()+",";
}
}
mContact = getMap(test);
newContactRequest();
break;
}
}
public void onClick(视图v){
开关(v.getId()){
案例R.id.btn_创建:
字符串测试=”;
对于(int i=0;i<(此总数);i+=2){
TextView电视=(TextView)v.findViewById(i++);
EditText et=(EditText)v.findViewById(i+2);
如果((i+2)>=此总数){
test+=tv.getText()+“=”+et.getText();
}否则
{
test+=tv.getText()+“=”+et.getText()+“,”;
}
}
mContact=getMap(测试);
newContactRequest();
打破
}
}
谢谢你的帮助 您需要显式地将数据类型更改为字符串。仅仅编写i++就试图将该值赋值为整数。 我想如果你把台词改成这些应该行
TextView tv = (TextView) v.findViewById(String.valueOf(i++));
EditText et = (EditText) v.findViewById(String.valueOf(i+2));
您需要显式地将数据类型更改为字符串。仅仅编写i++就试图将该值赋值为整数。 我想如果你把台词改成这些应该行
TextView tv = (TextView) v.findViewById(String.valueOf(i++));
EditText et = (EditText) v.findViewById(String.valueOf(i+2));