【发布时间】:2016-07-22 10:42:51
【问题描述】:
我有一个按钮,每次点击时都会创建一个标签,假设我点击了 10 次,所以现在我创建了 10 个标签。如何删除我点击的随机标签?
PS: my labels are added to a stackLayout
我的应用程序的想法是创建一个待办事项列表。我有一个输入框和一个按钮。我在输入框中输入我想要执行的操作,然后单击按钮并创建一个标签,我刚刚输入该标签。当我完成某件事后,我想点击特定标签并将其删除,这怎么可能。有什么帮助吗?
代码:
var entry = new Entry();
entry.Placeholder = "type here";
entry.BackgroundColor = Color.White;
entry.PlaceholderColor = Color.Gray;
var newButton = new Button { Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), };
StackLayout stackLayout = new StackLayout();
stackLayout.Children.Add(entry);
stackLayout.Children.Add(newButton);
this.Content = stackLayout;
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += labelClick;
newButton.Clicked += (sender, args) => { label = new Label();
label.BackgroundColor = Color.White;
label.TextColor = Color.Black;
label.Text = entry.Text;
entry.Text = "";
stackLayout.Children.Add(label);
label.GestureRecognizers.Add(tapGestureRecognizer);
【问题讨论】:
-
拜托,你能添加一些你迄今为止尝试过的代码示例吗?
-
var entry = new Entry(); entry.Placeholder = "在这里输入"; entry.BackgroundColor = 颜色.白色; entry.PlaceholderColor = 颜色.灰色; var newButton = new Button { Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), }; StackLayout stackLayout = new StackLayout(); stackLayout.Children.Add(entry); stackLayout.Children.Add(newButton); this.Content = stackLayout;
-
var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += labelClick; newButton.Clicked += (sender, args) => { label = new Label(); label.BackgroundColor = 颜色.白色; label.TextColor = 颜色.黑色;标签.Text = entry.Text; entry.Text = ""; stackLayout.Children.Add(标签);标签.GestureRecognizers.Add(tapGestureRecognizer); };
标签: xamarin.forms labels uitapgesturerecognizer