首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何检索元素父容器的属性

如何检索元素父容器的属性
EN

Stack Overflow用户
提问于 2019-10-24 15:18:27
回答 1查看 59关注 0票数 0

我在一个线性布局中有多个卡片视图,在每个卡片中有两个复选框。如何获取复选框所在容器(Cardview)的文本属性?

我仔细阅读了Xamarin.Android文档,看看是否有方法可以访问元素的父元素或容器。我看到Xamarin.Forms有一个element类,可以用来访问元素的父元素。

我已经尝试从Cardview捕获文本并将其放入字符串中,这不会起作用,因为文本可能会根据选中复选框的cardview而有所不同。

代码语言:javascript
运行
AI代码解释
复制
//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() + "\n";
   }

CheckBox[] checkBoxesFillings = { checkFillingSugar, checkFillingLemonJuice, checkFillingGoldenSyrup, checkFillingMarmite, checkFillingStrawberries, checkFillingChocolateSpread, checkFillingBlueberries };
fillingsReturned = CheckDetails(checkBoxesFillings);
string fillings = "";
foreach (CheckBox fillingbox in fillingsReturned)
   {
    fillings = fillings + fillingbox.Text.ToString() + "\n";
   }

//This toast is for testing purposes only
string order = "The following has been added to your order:\n" +
pancakeQuantitySpnr.SelectedItem + "\n" +
"Pancake type: " + pancakeTypeSpnr.SelectedItem + "\n" +
"Pancake style: " + pancakeStyleSpnr.SelectedItem + "\n" +
"Fillings: " + "\n" + fillings +
"Toppings: " + "\n" + 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;
}

当单击order按钮时,我希望BtnOrder_Click方法代码能够分辨出点击了to /filling复选框的是哪张卡(即糖、巧克力)。

EN

回答 1

Stack Overflow用户

发布于 2019-10-28 05:16:18

我已经尝试从Cardview中捕获文本并将其放入字符串中,但这不起作用,因为文本可能会因选中复选框的cardview而有所不同。

我可以像下面这样获取cardview的文本。当选中复选框并更改文本时,它可以很好地工作。

1.根据TextViews的id获取文本

代码语言:javascript
运行
AI代码解释
复制
var str = textView.Text;

您提供的代码没有显示如何更改文本,我使用下面的代码进行测试。文本视图位于cardSugar卡片视图中。

代码语言:javascript
运行
AI代码解释
复制
  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所有的线性布局元素获取文本。它需要与您定义的布局相匹配。你可以参考我的项目中的布局。我在输出窗口中输出结果。

代码语言:javascript
运行
AI代码解释
复制
  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);
                        }
                    }
                }

            }

当点击order按钮时,我希望BtnOrder_Click方法代码能够分辨出点击了to /filling复选框的是哪张卡(即糖、巧克力)。

我建议您将复选框放入Cardview中,这样我们就可以将它们连接起来。当您选中下面的复选框时,您可以获得卡片视图。

代码语言:javascript
运行
AI代码解释
复制
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输出了两次,因为我选中了cardsuger的两个复选框(Topping和Filling)。

您可以从GitHub的Cardview/AndroidDemo文件夹下载以供参考。https://github.com/WendyZang/Test.git

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58544394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文