gpt4 book ai didi

android - 获取动态添加的 View 的 ID

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:40 25 4
gpt4 key购买 nike

如何获取布局中动态添加的View的ID?

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

现在我想通过检查其类型来设置我获得的 View 的 ID?我该怎么做?

最佳答案

您可以使用 View 的 getId() 和 setId() 方法来完成。

例如:

TextView txtView = new TextView(this);
txtView.setId(4);

您可以使用 getId() 方法获取 ID:

txtView.getId();

关于android - 获取动态添加的 View 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6746313/

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