android - 如何在xamarin android中点击动态添加的textview?因为每当我点击 textview 它只显示最后一项文本,下面是我的代码
问题描述
TableRow row;
TextView txt;
LinearLayout tl = FindViewById<LinearLayout>(Resource.Id.tableLayout);
var servicesData = DataServices.GetAllServices();
try
{
foreach (var item in servicesData)
{
row = new TableRow(this);
imgBtn = new ImageButton(this);
txt = new TextView(this) {Id=item.ServiceId };
btntxt = new Button(this);
txt.SetOnTouchListener(new View.IOnTouchListener)
String imgUrl = "https://careservicesapi.boffincoders.com/Images/" + item.ServiceIcon;
var imageBmp = GetImageBitmapFromUrl(imgUrl);
Drawable drawable = new BitmapDrawable(Resources, imageBmp);
Drawable drawable1 = new BitmapDrawable();
drawable.SetBounds(0, 0, 50, 40);
txt.SetMinimumHeight(100);
txt.SetMinimumWidth(100);
txt.SetHeight(100);
txt.TextSize = 10;
txt.Gravity = GravityFlags.Center;
txt.Text = item.ServiceName;
txt.Tag = item.ServiceId;
txt.Click += Txt_Click;
txt.SetCompoundDrawablesWithIntrinsicBounds(drawable1, drawable, drawable1, drawable1);
txt.Clickable = true;
tl.AddView(row);
row.AddView(txt);
}
}
private void Txt_Click(object sender, EventArgs e)
{
var q = this.txt.Text;
}
解决方案
它仅显示最后一项文本
因为您正在显示 的文本txt
,所以它是最后一项。请从sender
.
例如:
private void Txt_Click(object sender, System.EventArgs e)
{
var q = ((TextView)sender).Text;
Toast.MakeText(this, q, ToastLength.Short).Show();
}
推荐阅读
- javascript - Google 地图:使用自定义标记和自定义地图样式生成 Web 嵌入代码?
- ruby - 在 Ruby 2.3.3 中查找哪个小部件具有焦点
- getopt - getopt 中的 optstring 是否区分大小写?
- azure - 问题 Azure 翻译 API 是当我想用一些特殊字符(如 %、=、=>、# API 响应 500 Internal server error)翻译文本时
- c# - EF Core - 使用 ForeignKey 引用父级的父级
- swift - 在故事板之间切换,从应用程序到框架
- clojure - * clojure.spec 中的正则表达式运算符
- angular - 当用户重定向到登录时如何保留最后一条路线-by Ngrx Effect angular
- powershell - 在 powershell 2.0 中执行 Get-Module 时出错
- python - Skype面试api自动调度不起作用