简体   繁体   English

带有单击事件的Xamarin Android创建按钮

[英]Create button with click event Xamarin Android

I have the following item.xml - 我有以下item.xml-

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
  <TextView
      android:id="@+id/txtTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

  <Button
      android:id="@+id/[id required here for click event.]"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

</LinearLayout>

Using this layout template I am creating multiple items in the code behind - 使用此布局模板,我在后面的代码中创建了多个项目-

    public Item(string title, Button btn)
    {
        Text = title;
        Button = btn;
    }

    public override View GetView(int position, View convertView)
    {
        var item = Items[position];
        var view = convertView;

        var contentItem = (Item)item;
        view = _inflater.Inflate(Resource.Layout.ListViewContentItem, null);

        var title = view.FindViewById<TextView>(Resource.Id.txtTitle);
        var btn = view.FindViewById<Button>([button id??]);

        title.Text = contentItem.Text;
        btn = contentItem.Button;

        return view;
    }

My understanding is that id's have to be unique, so how can I create buttons with unique id's so that I can access their click events? 我的理解是ID必须是唯一的,那么如何创建具有唯一ID的按钮才能访问其单击事件?

My code would have to be able to handle the creation of multiple Items each with their own buttons. 我的代码必须能够处理具有各自按钮的多个项的创建。

That is simple you use id the same way as you used for TextView 这很简单,您使用id就像使用TextView一样

So your button would look something like this: 因此,您的按钮将如下所示:

 <Button
  android:id="@+id/buttonId"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

Now in your click event, you can use the Position Parameter to find out which element of the list is receiving the click even at eg: 现在,在您的click事件中,您甚至可以在以下位置使用Position Parameter找出列表中的哪个元素正在获得点击:

  var btn = view.FindViewById<Button>(Resource.Id.buttonId);
  btn.Clicked+= (s,e)=>
  {
    if(position==someValue)
    {//Code}
    else
    {//Code}
  };

You can write this code in GetView method : 您可以在GetView方法中编写以下代码:

 var btn = view.FindViewById<Button>(Resource.Id.buttonId);
    btn.Tag=position;
    btn.SetOnClickListener(this);

after implement "View.IOnClickListener" in your adapter and override Onclick method 在您的适配器中实现“ View.IOnClickListener”并覆盖Onclick方法之后

public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.buttonId:
                   //Write code here
                    break;

            }

        }

In your XML: 在您的XML中:

<Button
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

And for you activity: 而对于您的活动:

Button btn = view.FindViewById<Button>(Resource.Id.btn);
btn.Clicked+= btn_click;

private void LiveLeadSearch_Click(object sender, EventArgs e)
{
   //Your implementation
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM