gpt4 book ai didi

java - 如何在android中点击动态创建的编辑文本时获取编辑文本的ID?

转载 作者:行者123 更新时间:2023-12-01 13:00:03 25 4
gpt4 key购买 nike

我正在根据项目要求在布局中动态创建 View 。 View 序列来自 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();
}
});
}

}

在此代码中,alist_interface_type 是包含我的 View 的数组列表。如果此列表包含“时间”,则创建编辑文本并单击编辑文本调用时间选择器方法来选择时间并设置为编辑文本。我给出 i 的值编辑文本的 id。单击​​编辑文本时,它不会提供特定单击的编辑文本的 id 来设置值。 enter image description here

最佳答案

// 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);
}

关于java - 如何在android中点击动态创建的编辑文本时获取编辑文本的ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com