如何解决我的ListView有两个或三个SetOnClickListener

问题描述 投票:0回答:2

我在xamarin Android的初学者,正在寻找一种方法,使我的代码工作的意愿,

我的代码包含每次点击3个TextView的标题,主要历史也有两个按钮,分享按钮和反

public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate(Resource.Layout.list_ticket_view, null);
            view.FindViewById<TextView>(Resource.Id.titletxt).Text = item.title;
            view.FindViewById<TextView>(Resource.Id.maintxt).Text = item.main;
            view.FindViewById<TextView>(Resource.Id.txthistory).Text = item.history;

            var share_butn = view.FindViewById<ImageView>(Resource.Id.share_butn);



            var button1 = view.FindViewById<Button>(Resource.Id.button1);
            button1.Text = (item.counter).ToString();
            button1.SetOnClickListener(this);


            return view;
        }



        public void OnClick(View v)
        {
            var button = v as Button;
            int count = int.Parse(button.Text) - 1;
            //new two line under this:
            button.Text = count.ToString();
            if (count < 0)
            {
                count = 0;
                button.Clickable = false;

            }
            button.Text = count.ToString();

        }

我的柜台的工作很好,但我不能找到办法用的shared_ptr很好地工作

更新:我瓦纳把下面的代码里面的OnClick

Intent intentsend = new Intent();
                        intentsend.SetAction(Intent.ActionSend);
                        intentsend.PutExtra(Intent.ExtraText,item.title+"\n"+item.main+"\n"+item.history);
                        intentsend.SetType("text/plain");
                        context.StartActivity(intentsend);
c# android xamarin xamarin.android
2个回答
1
投票

你可以ALSE点击lisetener添加到ImageView的:

share_butn.SetOnClickListener(this);
share_butn.Tag = position;

然后在OnClick,你可以告诉它是否通过ID是share_butnor按钮:

public void OnClick(View v)
    {
       if(v.Id == Resource.Id.share_butn){
         int position = (int)v.Tag;
         Intent intentsend = new Intent();
         intentsend.SetAction(Intent.ActionSend);
         intentsend.PutExtra(Intent.ExtraText,item[position].title+"\n"+item[position].main+"\n"+item[position].history);
         intentsend.SetType("text/plain");
         context.StartActivity(intentsend);          
       }
       if(v.Id == Resource.Id.button1){
         var button = v as Button;
        int count = int.Parse(button.Text) - 1;
        //new two line under this:
        button.Text = count.ToString();
        if (count < 0)
        {
            count = 0;
            button.Clickable = false;

        }
        button.Text = count.ToString();
       }


    }

0
投票

由于您使用的Xamarin本地AndroidXamarin.Android都有自己的监听点击事件的方式,我会建议你使用:

button1.Click+= HandleClick; // Applying the event

private void HandleClick(object sender, EventArgs e)
{
  // Click event code
}

更新:

尝试lambda表达式,而不是上面的代码为您的方案

 button1.Click+=(s, e) => {
    // Click event code
   };
© www.soinside.com 2019 - 2024. All rights reserved.