如果我以编程方式创建视图,我想知道如何引用它。
我必须使用“添加乘客”和“删除乘客”按钮分别为我的应用程序创建新的乘客视图。promts 保存为名为“@+id/passenger”的线性布局,其中有两个名为“@+id/passenger_name”和“@+id/passenger_weight”的 EditText。然后将它们保存在另一个名为passenger_layout的父LinearLayout中,它可以将所有乘客LinearLayouts放在一堆中
添加新乘客很容易,但我不知道如何引用新创建的元素。我猜他们会自动获得某种标识符?我希望它们是“passenger_name%”和“passenger_weight%”,其中 % 是索引 _passengerCount。
addPassenger.Click += delegate {
//Add to index
++passengerCount;
//Prep new passenger layout
var newLayout = new LinearLayout(Activity);
//Set LayoutParameters from the existing passenger LayoutParameters
newLayout.LayoutParameters = newPassenger.LayoutParameters;
//Prep the new EditTexts
var name = new EditText(Activity);
var weight = new EditText(Activity);
//Set the EditTexts' LayoutParameters from existing LayoutParameters
name.LayoutParameters = new ViewGroup.LayoutParams(passengerName.LayoutParameters);
weight.LayoutParameters = new ViewGroup.LayoutParams(passengerWeight.LayoutParameters);
//These cleary don't work :<
// name.Id = Resources.GetIdentifier( "passenger_name" + passengerCount, "id", Activity.PackageName);
// weight.Id = "passenger_weight" + passengerCount;
//Add EditTexts to the new passenger layout and then and then add the new passenger to the parent LinearLayout
newLayout.AddView(name);
newLayout.AddView(weight);
passengerLayout.AddView(newLayout);
Log.Debug(GetType().FullName, "Add clicked");
};
那是我创建新乘客的点击委托,但即使我像这样创建它们,我也不知道如果我必须例如删除它们或获取名称或体重数据,我以后如何找到它们。
如何引用以编程方式创建的 UI 元素?