如何检索元素父容器的属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检索元素父容器的属性相关的知识,希望对你有一定的参考价值。
我在线性布局中有多个卡片视图,每个卡片中都有两个复选框。如何获取复选框所在的容器(Cardview)的文本属性?
我已经仔细检查了Xamarin.android文档,以查看是否可以访问元素的父级或容器。我看到Xamarin.Forms有一个Element类,可用于访问元素的父级。
我已经尝试从Cardview捕获文本并将其放入字符串,这将无法工作,因为文本可能会因选中复选框的Cardview而有所不同。
//Here are my initialisers
btnOrder = FindViewById<Button>(Resource.Id.btnOrder);
pancakeTypeSpnr = FindViewById<Spinner>(Resource.Id.pancaketypespnr);
pancakeStyleSpnr = FindViewById<Spinner>(Resource.Id.pancakestylespnr);
pancakeQuantitySpnr = FindViewById<Spinner>(Resource.Id.pancakequantityspnr);
cardSugar = FindViewById<CardView>(Resource.Id.cardSugar);
cardSugar.Click += CardSugar_Click;
cardLemonJuice = FindViewById<CardView>(Resource.Id.cardLemonJuice);
cardLemonJuice.Click += CardLemonJuice_Click;
cardGoldenSyrup = FindViewById<CardView>(Resource.Id.cardGoldenSyrup);
cardGoldenSyrup.Click += CardGoldenSyrup_Click;
cardMarmite = FindViewById<CardView>(Resource.Id.cardMarmite);
cardMarmite.Click += CardMarmite_Click;
cardStrawberries = FindViewById<CardView>(Resource.Id.cardStrawberries);
cardStrawberries.Click += CardStrawberries_Click;
cardChocolateSpread = FindViewById<CardView>(Resource.Id.cardChocolateSpread);
cardChocolateSpread.Click += CardChocolateSpread_Click;
cardBlueberries = FindViewById<CardView>(Resource.Id.cardBlueberries);
cardBlueberries.Click += CardBlueberries_Click;
//Topping checkbox reference
checkToppingSugar = FindViewById<CheckBox>(Resource.Id.checkToppingSugar);
checkToppingLemonJuice = FindViewById<CheckBox>(Resource.Id.checkToppingLemonJuice);
checkToppingGoldenSyrup = FindViewById<CheckBox>(Resource.Id.checkToppingGoldenSyrup);
checkToppingMarmite = FindViewById<CheckBox>(Resource.Id.checkToppingMarmite);
checkToppingStrawberries = FindViewById<CheckBox>(Resource.Id.checkToppingStrawberries);
checkToppingChocolateSpread = FindViewById<CheckBox>(Resource.Id.checkToppingChocolateSpread);
checkToppingBlueberries = FindViewById<CheckBox>(Resource.Id.checkToppingBlueberries);
//Filling checkbox reference
checkFillingSugar = FindViewById<CheckBox>(Resource.Id.checkFillingSugar);
checkFillingLemonJuice = FindViewById<CheckBox>(Resource.Id.checkFillingLemonJuice);
checkFillingGoldenSyrup = FindViewById<CheckBox>(Resource.Id.checkFillingGoldenSyrup);
checkFillingMarmite = FindViewById<CheckBox>(Resource.Id.checkFillingMarmite);
checkFillingStrawberries = FindViewById<CheckBox>(Resource.Id.checkFillingStrawberries);
checkFillingChocolateSpread = FindViewById<CheckBox>(Resource.Id.checkFillingChocolateSpread);
checkFillingBlueberries = FindViewById<CheckBox>(Resource.Id.checkFillingBlueberries);
The following is my Order button code
//Order button... Completes initial check to see if compulsary boxes have been checked, then compiles order
private void BtnOrder_Click(object sender, EventArgs e)
{
if (pancakeTypeSpnr.SelectedItemPosition == 0)
{
Toast.MakeText(this, "You must select a pancake type!",
ToastLength.Short).Show();
}
else if (pancakeStyleSpnr.SelectedItemPosition == 0)
{
Toast.MakeText(this, "You must select a pancake style!",
ToastLength.Short).Show();
}
else if (pancakeQuantitySpnr.SelectedItemPosition == 0)
{
Toast.MakeText(this, "You must order at least one pancake",
ToastLength.Short).Show();
}
else
{
/*This section takes all the checkboxes and checks them to see if they are checked. If they are, they are put into an array of checked boxes*/
CheckBox[] checkBoxesToppings = { checkToppingSugar, checkToppingLemonJuice, checkToppingGoldenSyrup, checkToppingMarmite, checkToppingStrawberries, checkToppingChocolateSpread, checkToppingBlueberries };
toppingsReturned = CheckDetails(checkBoxesToppings);
string toppings = "";
foreach (CheckBox toppingbox in toppingsReturned)
{
toppings = toppings + toppingbox.Tag.ToString() + "
";
}
CheckBox[] checkBoxesFillings = { checkFillingSugar, checkFillingLemonJuice, checkFillingGoldenSyrup, checkFillingMarmite, checkFillingStrawberries, checkFillingChocolateSpread, checkFillingBlueberries };
fillingsReturned = CheckDetails(checkBoxesFillings);
string fillings = "";
foreach (CheckBox fillingbox in fillingsReturned)
{
fillings = fillings + fillingbox.Text.ToString() + "
";
}
//This toast is for testing purposes only
string order = "The following has been added to your order:
" +
pancakeQuantitySpnr.SelectedItem + "
" +
"Pancake type: " + pancakeTypeSpnr.SelectedItem + "
" +
"Pancake style: " + pancakeStyleSpnr.SelectedItem + "
" +
"Fillings: " + "
" + fillings +
"Toppings: " + "
" + toppings;
Toast.MakeText(this, order, ToastLength.Long).Show();
}
}
private CheckBox[] CheckDetails(CheckBox[] details)
{
var checkedCheckboxes = new List<CheckBox>();
foreach (CheckBox cb in details)
{
if (cb.Checked == true)
{
checkedCheckboxes.Add(cb);
}
else continue;
}
CheckBox[] ticked = checkedCheckboxes.ToArray();
return ticked;
}
[单击订购按钮时,我希望BtnOrder_Click方法代码能够告诉您单击了顶部/填充复选框是哪张卡(即,糖,巧克力酱)。
我已经尝试从Cardview中捕获文本并将其放入字符串中,这将无法工作,因为文本可能会因选中复选框的Cardview的不同而有所不同。
我可以得到如下cardview的文本。当选中此复选框并更改了文本时,它可以很好地工作。
1。根据您的TextViews的ID获取文本
var str = textView.Text;
您提供的代码未显示如何更改文本,我使用下面的代码进行测试。文本视图位于cardSugar Cardview中。
var textView = FindViewById<TextView>(Resource.Id.textView);
var checkToppingSugar = FindViewById<CheckBox>(Resource.Id.checkToppingSugar);
checkToppingSugar.Click += (o, e) =>
{
if (checkToppingSugar.Checked)
{
Toast.MakeText(this, "Selected", ToastLength.Short).Show();
textView.Text = "textView Topping Selected";
}
else
{
Toast.MakeText(this, "Not selected", ToastLength.Short).Show();
textView.Text = "textView Topping Not selected";
}
};
2。通过foreach获取线性布局中的所有元素的文本。它需要匹配您定义的布局。 您可以在我的项目中参考布局。我在输出窗口上输出结果。
var linearLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout_view);
for (int i = 0; i < linearLayout.ChildCount; i++)
{
View child = linearLayout.GetChildAt(i);
if (child.GetType() == typeof(CardView))
{
var viewGroup = ((ViewGroup)child).GetChildAt(0);
for (int j = 0; j < ((ViewGroup)viewGroup).ChildCount; j++)
{
var viewGroup2 = ((ViewGroup)viewGroup).GetChildAt(j);
if (viewGroup2.GetType() == typeof(TextView))
{
System.Diagnostics.Debug.WriteLine(viewGroup2.GetType().ToString() + ": " + ((TextView)viewGroup2).Text);
}
}
}
}
[单击订购按钮时,我希望BtnOrder_Click方法代码能够告诉您单击了顶部/填充复选框是哪张卡(即,糖,巧克力酱)。
我建议您将复选框放在Cardview内,以便我们可以将它们都连接起来。当您选中以下复选框时,您可以获取卡片视图。
for (int i = 0; i < linearLayout.ChildCount; i++)
{
View child = linearLayout.GetChildAt(i);
if (child.GetType() == typeof(CardView))
{
var viewGroup = ((ViewGroup)child).GetChildAt(0);
for (int j = 0; j < ((ViewGroup)viewGroup).ChildCount; j++)
{
var viewGroup2 = ((ViewGroup)viewGroup).GetChildAt(j);
if (viewGroup2.GetType() == typeof(CheckBox))
{
if (((CheckBox)viewGroup2).Checked)
{
System.Diagnostics.Debug.WriteLine(((CardView)child));
}
}
}
}
}
结果:上一次,cardsuger输出两次,因为我同时选中了carduger的两个复选框(打顶和填充)。
您可以从GitHub的Cardview / AndroidDemo文件夹下载以供参考。https://github.com/WendyZang/Test.git
以上是关于如何检索元素父容器的属性的主要内容,如果未能解决你的问题,请参考以下文章
CSS使容器相对父元素垂直水平居中定位(父容器及子元素大小不确定)