Xamarin.android 单击处理程序删除表格行

Xamarin.android 单击处理程序删除表格行,xamarin.android,android-tablelayout,Xamarin.android,Android Tablelayout,我正在构建一个应用程序,以编程方式将tablerows添加到视图中。我将deletebutton添加到作为ImageButton的每一行。现在我有几个问题 我可以使用ImageButton吗 如何获取tablerow id 单击Delete按钮的位置 如何转换事件参数 从clickhandler进入MenuItemClickEventArgs或反之亦然 反之亦然 我的clickhandler应该是什么样子 以下是我的源代码: public class CalculatorSide2 : Acti

我正在构建一个应用程序,以编程方式将tablerows添加到视图中。我将deletebutton添加到作为ImageButton的每一行。现在我有几个问题

  • 我可以使用ImageButton吗
  • 如何获取tablerow id 单击Delete按钮的位置
  • 如何转换事件参数 从clickhandler进入MenuItemClickEventArgs或反之亦然 反之亦然
  • 我的clickhandler应该是什么样子
  • 以下是我的源代码:

    public class CalculatorSide2 : Activity
    {
        private Button stepButton;
        private IntentHelper intentHelper = new IntentHelper();
        private string age;
        private string estLife;
        private string estPens;
        private string[] pensions;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            intentHelper.IntentSide2(Intent, out age, out estLife,out estPens);
    
            SetContentView(Resource.Layout.calculatorside2);
    
            TableLayout tl_layout = FindViewById<TableLayout>(Resource.Id.tableLayout1);
    
            ImageView plusButton = FindViewById<ImageView>(Resource.Id.plusButton);
            stepButton = FindViewById<Button>(Resource.Id.cside2stepButton);
    
            plusButton.Click += (sender, e) =>
            {
                PopupMenu popupMenu = new PopupMenu(this, plusButton);
                popupMenu.Inflate(Resource.Menu.popupmenu);
                fillPopupMenu(popupMenu);
                popupMenu.Show();
    
                popupMenu.MenuItemClick += (s1, arg) =>
                {
                    string info = arg.Item.TitleFormatted.ToString();
                    string id = arg.Item.ItemId.ToString();
    
                    var inputDialog = new AlertDialog.Builder(this);
    
                    EditText userInput = new EditText(this);
                    userInput.InputType = (Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
                    inputDialog.SetTitle(info);
                    inputDialog.SetView(userInput);
    
                    inputDialog.SetPositiveButton("Ok", (ss, ee) =>
                    {
                        TextView rowInfo = new TextView(this);
                        rowInfo.SetLines(2);
                        rowInfo.TextSize = 20;
                        rowInfo.SetTextColor(Android.Graphics.Color.Black);
                        rowInfo.Text = info + ": \n" + userInput.Text + "€";
    
                        ImageButton delete = new ImageButton(this);
                        delete.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.delete_icon));
    
                        TableRow row = new TableRow(this);
                        row.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                        row.SetBackgroundColor(Android.Graphics.Color.Rgb(255, 153, 0));
                        row.SetPadding(0, 0, 0, 30);
    
                        row.AddView(rowInfo);
                        row.AddView(delete);
                        tl_layout.AddView(row);
                    });
                    inputDialog.SetNegativeButton("Cancel", (se, es) => { });
    
                    inputDialog.Show();
                };
            };
    
            stepButton.Click += (object sender, EventArgs e) =>
            {
                var step = new Intent(this, typeof(CalculatorSide3));
                step.PutExtra("Age", age);
                step.PutExtra("EstPens", estPens);
                step.PutExtra("EstLife", estLife);
                step.PutStringArrayListExtra("Pensions", pensions);
                StartActivity(step);
            };
        }
    
    
        private void fillPopupMenu(PopupMenu menu)
        {
            int groupId = 0;
            int i = 0;
            int menuItemId = Android.Views.Menu.First;
            int menuItemOrder = Android.Views.Menu.None;
    
            foreach (var item in Enum.GetNames(typeof(PopupMenuItems)))
            {
                string itemString = item.ToString();
                menu.Menu.Add(groupId, menuItemId + i, menuItemOrder + i, itemString.Replace("_", " "));
                i++;
            }
        }
    }
    
    公共类计算器Side2:活动
    {
    私人按钮-步进按钮;
    私有IntentHelper IntentHelper=新IntentHelper();
    私弦时代;
    私人生活;
    私用钢笔;
    私人养老金;
    创建时受保护的覆盖无效(捆绑包)
    {
    base.OnCreate(bundle);
    意向书。意向书2(意向书,超越年龄,超越生命,超越钢笔);
    SetContentView(Resource.Layout.calculatorside2);
    TableLayout tl_layout=findviewbyd(Resource.Id.tableLayout1);
    ImageView plusButton=FindViewById(Resource.Id.plusButton);
    stepButton=FindViewById(Resource.Id.cside2stepButton);
    加号按钮。单击+=(发件人,e)=>
    {
    PopupMenu PopupMenu=新的PopupMenu(此按钮为plusButton);
    充气(Resource.Menu.popupMenu);
    填充弹出菜单(弹出菜单);
    Show();
    popupMenu.MenuItemClick+=(s1,arg)=>
    {
    string info=arg.Item.TitleFormatted.ToString();
    字符串id=arg.Item.ItemId.ToString();
    var inputDialog=new AlertDialog.Builder(此);
    EditText userInput=新的EditText(此);
    userInput.InputType=(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
    inputDialog.SetTitle(信息);
    设置视图(用户输入);
    inputDialog.SetPositiveButton(“确定”,(ss,ee)=>
    {
    TextView rowInfo=新的TextView(此);
    行信息设置行(2);
    rowInfo.TextSize=20;
    rowInfo.SetTextColor(Android.Graphics.Color.Black);
    rowInfo.Text=info+“:\n”+userInput.Text+“€”;
    ImageButton delete=新建ImageButton(此);
    delete.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.delete_图标));
    TableRow row=新的TableRow(本);
    row.LayoutParameters=新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent);
    SetBackgroundColor(Android.Graphics.Color.Rgb(255,153,0));
    设置填充(0,0,0,30);
    row.AddView(rowInfo);
    行。添加视图(删除);
    tl_布局。添加视图(行);
    });
    SetNegativeButton(“取消”,(se,es)=>{};
    inputDialog.Show();
    };
    };
    单步按钮。单击+=(对象发送者,事件参数e)=>
    {
    var步骤=新意图(此,类型为(CalculatorSide3));
    步骤.额外(“年龄”,年龄);
    步骤.PutExtra(“EstPens”,EstPens);
    步骤.PutExtra(“EstLife”,EstLife);
    步骤.PutStringArrayListExtra(“养老金”,养老金);
    星触觉(阶跃);
    };
    }
    私有void fillPopupMenu(PopupMenu菜单)
    {
    int-groupId=0;
    int i=0;
    int menuItemId=Android.Views.Menu.First;
    int menuItemOrder=Android.Views.Menu.None;
    foreach(Enum.GetNames(typeof(PopupMenuItems))中的变量项)
    {
    string itemString=item.ToString();
    menu.menu.Add(groupId,menuItemId+i,menuItemOrder+i,itemString.Replace(““,”);
    i++;
    }
    }
    }
    
    我希望有人能理解我的意思,卑诗省英语不是我的母语。

    公共课计算器side2:Activity
    
    public class CalculatorSide2 : Activity
    {
        private Button stepButton;
        private IntentHelper intentHelper = new IntentHelper();
        private string age;
        private string estLife;
        private string estPens;
        private string[] pensions;
        private Dictionary<int,string> temp;
        private TableLayout tl_layout;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            intentHelper.IntentSide2(Intent, out age, out estLife,out estPens);
    
            SetContentView(Resource.Layout.calculatorside2);
    
            tl_layout = FindViewById<TableLayout>(Resource.Id.tableLayout1);
    
            ImageView plusButton = FindViewById<ImageView>(Resource.Id.plusButton);
            stepButton = FindViewById<Button>(Resource.Id.cside2stepButton);
    
            temp = new Dictionary<int,string>();
            int i = 0;
    
            plusButton.Click += (sender, e) =>
            {
                PopupMenu popupMenu = new PopupMenu(this, plusButton);
                popupMenu.Inflate(Resource.Menu.popupmenu);
                fillPopupMenu(popupMenu);
                popupMenu.Show();
    
                popupMenu.MenuItemClick += (s1, arg) =>
                {
                    string info = arg.Item.TitleFormatted.ToString();
                    string id = arg.Item.ItemId.ToString();
    
                    var inputDialog = new AlertDialog.Builder(this);
    
                    EditText userInput = new EditText(this);
                    userInput.InputType = (Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
                    inputDialog.SetTitle(info);
                    inputDialog.SetView(userInput);
    
                    inputDialog.SetPositiveButton("Ok", (ss, ee) =>
                    {
                        TextView rowInfo = new TextView(this);
                        rowInfo.SetLines(2);
                        rowInfo.TextSize = 20;
                        rowInfo.SetTextColor(Android.Graphics.Color.Black);
                        rowInfo.Text = info + ": \n" + userInput.Text + "€";
    
                        ImageButton delete = new ImageButton(this);
                        delete.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.delete_icon));
                        delete.Focusable = false;
                        delete.Clickable = false;
    
                        TableRow row = new TableRow(this);
                        row.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                        row.SetBackgroundColor(Android.Graphics.Color.Rgb(255, 153, 0));
                        row.SetPadding(0, 0, 0, 30);
                        row.Id = 10 + i;
    
                        row.Click += new EventHandler(HandleClick);
    
                        row.AddView(rowInfo);
                        row.AddView(delete);
                        tl_layout.AddView(row);
    
                        temp.Add(row.Id,userInput.Text);
    
                        i++;
                    });
                    inputDialog.SetNegativeButton("Cancel", (se, es) => { });
    
                    inputDialog.Show();
                };
            };
    
            stepButton.Click += (object sender, EventArgs e) =>
            {
                if (temp.Count != 0)
                {
                    pensions = new string[temp.Count];
                    var j = 0;
                    foreach (KeyValuePair<int, string> pair in temp)
                    {
                        pensions[j] = pair.Value;
                        j++;
                    }
                }
                else
                {
                    pensions = new string[0];
                }
    
                var step = new Intent(this, typeof(CalculatorSide3));
                step.PutExtra("Age", age);
                step.PutExtra("EstPens", estPens);
                step.PutExtra("EstLife", estLife);
                step.PutStringArrayListExtra("Pensions", pensions);
                StartActivity(step);
            };
        }
    
        private void fillPopupMenu(PopupMenu menu)
        {
            int groupId = 0;
            int i = 0;
            int menuItemId = Android.Views.Menu.First;
            int menuItemOrder = Android.Views.Menu.None;
    
            foreach (var item in Enum.GetNames(typeof(PopupMenuItems)))
            {
                string itemString = item.ToString();
                menu.Menu.Add(groupId, menuItemId + i, menuItemOrder + i, itemString.Replace("_", " "));
                i++;
            }
        }
    
        public void HandleClick(object sender, EventArgs e)
        {
            var clickedTR = sender as TableRow;
            int trId = clickedTR.Id;
    
            var builder = new AlertDialog.Builder(this);
            builder.SetTitle("Löschen");
            builder.SetMessage("Möchten Sie den Eintrag wirklich löschen?");
            builder.SetPositiveButton("Ja", (ssr,args) => {
                temp.Remove(trId);
                tl_layout.RemoveView(clickedTR);
            });
            builder.SetNegativeButton("Nein",(sse,arge) => { });
            builder.SetCancelable(false);
            builder.Show();
        }
    }
    
    { 私人按钮-步进按钮; 私有IntentHelper IntentHelper=新IntentHelper(); 私弦时代; 私人生活; 私用钢笔; 私人养老金; 私人词典; 私人桌面布局tl_布局; 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bundle); 意向书。意向书2(意向书,超越年龄,超越生命,超越钢笔); SetContentView(Resource.Layout.calculatorside2); tl_layout=findviewbyd(Resource.Id.tableLayout1); ImageView plusButton=FindViewById(Resource.Id.plusButton); stepButton=FindViewById(Resource.Id.cside2stepButton); temp=新字典(); int i=0; 加号按钮。单击+=(发件人,e)=> { PopupMenu PopupMenu=新的PopupMenu(此按钮为plusButton); 充气(Resource.Menu.popupMenu); 填充弹出菜单(弹出菜单); Show(); popupMenu.MenuItemClick+=(s1,arg)=> { string info=arg.Item.TitleFormatted.ToString(); 字符串id=arg.Item.ItemId.ToString(); var inputDialog=new AlertDialog.Builder(此); EditText userInput=新的EditText(此); userInput.InputType=(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber); inputDialog.SetTitle(信息); 设置视图(用户输入); inputDialog.SetPositiveButton(“确定”,(ss,ee)=> { TextView rowInfo=新的TextView(此); 行信息设置行(2); rowInfo.TextSize=20; rowInfo.SetTextColor(Android.Graphics.Color.Black); rowInfo.Text=info+“:\n”+userInput.Text+“€”; 图像按钮d