我根据项目要求在布局中创建动态视图。视图的顺序来自json。当我点击编辑文本然后调用timeppicker函数在编辑文本中设置时间,但是当我设置编辑文本值时,它给出了最后创建的编辑文本的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();
}
});
}
}
在此代码中,alist_interface_type是包含我的视图的arraylist。如果此列表包含&#34; time&#34;然后创建编辑文本并单击编辑文本调用时间选择器方法以选择时间并设置为编辑文本。我将i的值赋予编辑文本的id。单击编辑文本它不会给出id特别点击编辑文本来设置值。
答案 0 :(得分:1)
// try this way,hope this will help you...
et2.setTag(et2.getId());
et2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
Log.d("id of edit text", ""+et2.getId());
timePicker(new OnTimeSelected(){
public void onSelect(String time){
((EditText)v.getTag()).setText(time);
}
});
}
});
interface OnTimeSelected{
public void onSelect(String time);
}