Clickhandler删除一个tablerow

时间:2014-05-23 10:11:44

标签: xamarin.android android-tablelayout

我正在建立一个应用程序,我以编程方式将视图添加到视图中。我为每一个ImageButton行添加了一个删除按钮。现在我有几个问题。

  1. 我可以使用ImageButton吗?
  2. 我如何获得tablerow id 点击删除按钮的位置?
  3. 我如何转换eventargs 从clickhandler到MenuItemOnMenuItemClickEventArgs或者副 反之亦然?
  4. 我的点击处理器应该如何?
  5. 这是我的源代码:

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

    我希望有人理解我的意思,bc英语不是我的母语。

1 个答案:

答案 0 :(得分:0)

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