我正在根据项目要求在布局中动态创建视图。视图序列来自 json 。当我单击编辑文本时,时间选择器函数会调用在编辑文本中设置时间,但是当我设置编辑文本值时,它会给出上次创建的编辑文本的 ID。
这是我的代码:-
for (i = 0; i < alist_interface_type.size(); i++) {
if(alist_interface_type.get(i).equals("textbox")) {
LinearLayout lLayout = new LinearLayout(getActivity());
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.setPadding(5, 5, 5, 5);
lLayout.setBackgroundColor(Color.parseColor("#E0E0E0"));
label = new TextView(getActivity());
label.setText(example.get(i));
label.setTextSize(16);
label.setTextColor((Color.parseColor("#01c534")));
label.setPadding(5, 5, 5, 5);
et = new EditText(getActivity());
et.setSingleLine(true);
et.setId(i);
et.setTextSize(18);
et.setPadding(5, 5, 5, 5);
if (!data_type.get(i).toString().equals("text")) {
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString()));
et.setFilters(filters);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
lLayout.addView(label, lp);
lLayout.addView(et, lp);
ll.addView(lLayout);
lp.setMargins(5, 0, 5, 5);
lLayout.setLayoutParams(lp);
alist_id.add(i);
alist_views.add("EditText");
}
else if (alist_interface_type.get(i).equals("time") {
LinearLayout lLayout = new LinearLayout(getActivity());
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.setPadding(5, 5, 5, 5);
lLayout.setBackgroundColor(Color.parseColor("#E0E0E0"));
label = new TextView(getActivity());
label.setText(example.get(i));
label.setTextSize(16);
label.setTextColor((Color.parseColor("#01c534")));
label.setPadding(5, 5, 5, 5);
et2 = new EditText(getActivity())
{
public boolean getDefaultEditable() {
return false;
}
};
et2.setSingleLine(true);
et2.setId(i);
et2.setTextSize(18);
Log.d("edit text cliked", ""+i);
et2.setText(pref.getString(alist_Label_name.get(i),""));
et2.setPadding(5, 5, 5, 5);
if (!data_type.get(i).toString().equals("text")) {
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString()));
et2.setFilters(filters);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
lLayout.addView(label, lp);
lLayout.addView(et2, lp);
lp.setMargins(5, 0, 5, 5);
lLayout.setLayoutParams(lp);
ll.addView(lLayout);
alist_id.add(i);
alist_views.add("time");
et2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("id of edit text", ""+et2.getId());
timePicker();
}
});
}
}
In this code alist_interface_type is arraylist that containing my views.If this list contains "time" then create edit text and click on edit text call time picker method for selecting time and set to edit text.I am giving the value of i to id of edit text.On click of edit text it doesn't give the id of particular clicked edit text to set the value .