本文介绍了获取ID为动态添加意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得被添加动态的布局视图的ID?
How to get ID of Views that are added dynamically in layout?
public class Display
{
static int i;
Dynamic_UIActivity dyn;
private Context context;
View mField= null;
TextView text;
protected int mYear;
protected int mMonth;
protected int mDay;
LinearLayout ll;
Mynewdialog dialognew;
public Display(Context mcontext)
{
context=mcontext;
ll = new LinearLayout(mcontext);
ll.setOrientation(LinearLayout.VERTICAL);
dyn = new Dynamic_UIActivity();
}
public void addLabel(String text)
{
TextView textview = new TextView(context);
textview.setText(text);
ll.addView(textview);
}
public void addField(String Type,String Tag)
{
mField.setId(i);
i++;
if(Type.equalsIgnoreCase("text"))
{
mField= new EditText(context);
mField.setTag(Tag);
//int id = mField.getResources().getIdentifier((String) mField.getTag(),"id" ,dyn.getPackageName());
//EditText edit = (EditText)mField.findViewById(1);
ll.addView(mField);
}
if(Type.equalsIgnoreCase("datepicker"))
{
mField=new EditText(context);
((EditText) mField).setText("Pick Date");
ll.addView(mField);
mField.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialognew = new Mynewdialog(context);
dialognew.dialog_obj((EditText) mField);
}
});
}
}
protected DatePickerDialogWithEditText.OnDateSetListener mdatepickerdialog = new DatePickerDialogWithEditText.OnDateSetListener()
{
public void onDateSet(DatePicker view , int year , int month, int date)
{
mYear = year;
mMonth = month;
mDay = date;
dialognew.showdialogtext();
}
};
}
class DatePickerDialogWithEditText extends DatePickerDialog
{
EditText tv;
DatePickerDialogWithEditText(Context c, OnDateSetListener oD, int y, int m, int d, EditText tv)
{
super(c, oD, y, m, d);
this.tv = tv;
}
EditText getAssociatedTextView()
{
return tv;
}
}
class Mynewdialog
{
private Context context;
Display dis = new Display(context);
private DatePickerDialogWithEditText dialog;
public Mynewdialog(Context mcontext)
{
context=mcontext;
}
public void dialog_obj(EditText editTextView)
{
final Calendar cal = Calendar.getInstance();
dis.mYear = cal.get(Calendar.YEAR);
dis.mMonth = cal.get(Calendar.MONTH);
dis.mDay = cal.get(Calendar.DAY_OF_MONTH);
dialog = new DatePickerDialogWithEditText(context, dis.mdatepickerdialog, dis.mYear, dis.mMonth, dis.mDay, editTextView);
dialog.show();
}
public void showdialogtext()
{
EditText et = dialog.getAssociatedTextView();
et.setText(new StringBuilder().append(dis.mDay).append("-").append(dis.mMonth).append("-").append(dis.mYear));
}
}
现在我要设置的,我得到通过检查其类型的意见,ID?我该怎么办呢?
推荐答案
您可以通过使用的getId()和SETID()查看方法做到这一点。
You can do it by using getId() and setId() methods of the View.
例如:
TextView txtView = new TextView(this);
txtView.setId(4);
和可以使用的getId得到的ID()方法:
and you can get the ID by using getId() method:
txtView.getId();
这篇关于获取ID为动态添加意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!