亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用點擊事件創建按鈕 Xamarin Android

使用點擊事件創建按鈕 Xamarin Android

C#
繁花不似錦 2022-11-21 20:11:07
我有以下 item.xml -<LinearLayoutandroid: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>使用此布局模板,我在后面的代碼中創建了多個項目 -    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;    }我的理解是 id 必須是唯一的,那么如何創建具有唯一 id 的按鈕以便我可以訪問它們的點擊事件?我的代碼必須能夠處理多個項目的創建,每個項目都有自己的按鈕。
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

這很簡單,您使用 id 的方式與您使用 TextView 的方式相同


所以你的按鈕看起來像這樣:


 <Button

  android:id="@+id/buttonId"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" />

現在在您的點擊事件中,您可以使用位置參數來找出列表中的哪個元素正在接收點擊,例如:


  var btn = view.FindViewById<Button>(Resource.Id.buttonId);

  btn.Clicked+= (s,e)=>

  {

    if(position==someValue)

    {//Code}

    else

    {//Code}

  };


查看完整回答
反對 回復 2022-11-21
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

在您的 XML 中:


<Button

  android:id="@+id/btn"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" />

對于你的活動:


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

btn.Clicked+= btn_click;


private void LiveLeadSearch_Click(object sender, EventArgs e)

{

   //Your implementation

}


查看完整回答
反對 回復 2022-11-21
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您可以在 GetView 方法中編寫此代碼:


 var btn = view.FindViewById<Button>(Resource.Id.buttonId);

    btn.Tag=position;

    btn.SetOnClickListener(this);

在您的適配器中實現“View.IOnClickListener”并覆蓋 Onclick 方法后


public void OnClick(View v)

        {

            switch (v.Id)

            {

                case Resource.Id.buttonId:

                   //Write code here

                    break;


            }


        }


查看完整回答
反對 回復 2022-11-21
  • 3 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號