mono for android Listview 裡面按鈕 view Button click 註冊方法 並且傳值給其他Activity 主要是context
阿新 • • 發佈:2018-12-27
需求:為Listview的Item裡面的按鈕Button新增一個事件,單擊按鈕時通過事件傳值並跳轉到新的頁面。
環境:mono
效果:
佈局程式碼
主佈局
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent"MainLayout5 android:layout_height="fill_parent"> 6 <ListView 7 android:id="@+id/myList" 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent" /> 10 </LinearLayout>
Item佈局
<?xml version="1.0" encoding="utf-8"?>ListviewItemlayout<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" /> </RelativeLayout>
MainActivity
1 using System; 2 3 using Android.App; 4 using Android.Content; 5 using Android.Runtime; 6 using Android.Views; 7 using Android.Widget; 8 using Android.OS; 9 using System.Collections.Generic; 10 11 namespace ListViewItemBtnClick 12 { 13 [Activity(Label = "ListViewItemBtnClick", MainLauncher = true, Icon = "@drawable/icon")] 14 public class Activity1 : Activity 15 { 16 ListView listView; 17 18 protected override void OnCreate(Bundle bundle) 19 { 20 base.OnCreate(bundle); 21 22 SetContentView(Resource.Layout.Main); 23 24 25 listView = FindViewById<ListView>(Resource.Id.myList); 26 27 28 //填充Listview 29 getList(listView); 30 31 32 33 } 34 35 //獲取Listview 36 private void getList(ListView listview) 37 { 38 39 Adapter goodsAdapter = new Adapter(this); 40 List<Item> itemList = new List<Item>(); 41 Item item = new Item(); 42 43 //給Item賦值 並且新增到list裡面 44 for (int i = 0; i <10; i++) 45 { 46 item.title = i + "|標題|"; 47 item.button = i + "btn"; 48 itemList.Add(item); 49 } 50 //給adapter中新增list的值 51 foreach (Item item1 in itemList) 52 { 53 goodsAdapter.Add(item1); 54 } 55 //將adapter的值付給listview 56 listview.Adapter = goodsAdapter; 57 } 58 59 } 60 }MainActivity
Item類
class Item { //標題 textview public string title { get; set; } //按鈕 public string button { get; set; } }Item
Adapter配置資料
1 namespace ListViewItemBtnClick 2 { 3 class Adapter:ArrayAdapter<Item>,View.IOnClickListener 4 { 5 6 private Context context; 7 8 public Adapter(Context context):base(context,0){ 9 //通過建構函式獲取context 10 this.context = context; 11 12 } 13 14 15 16 17 18 public override View GetView(int position, View convertView, ViewGroup parent) 19 { 20 View view; 21 //如果convertView不為空則直接使用 22 if (convertView!=null) 23 { 24 view = convertView; 25 26 } 27 28 else 29 { 30 view = LayoutInflater.From(this.Context).Inflate(Resource.Layout.ListItem, null); 31 32 } 33 34 //設定title的值 35 TextView title = view.FindViewById<TextView>(Resource.Id.txt); 36 Button btn = view.FindViewById<Button>(Resource.Id.btn); 37 38 Item item = GetItem(position); 39 40 title.Text = item.title + position; 41 //給每個按鈕新增一個tag tag的值為position 或者自定義的ID 42 btn.SetTag(Resource.Id.btn,position); 43 btn.Text ="--"+ position+"---"; 44 //註冊事件 45 btn.SetOnClickListener(this); 46 return view; 47 } 48 49 50 51 public void OnClick(View v) 52 { 53 //獲得點選的按鈕 54 Button btn = v as Button; 55 //獲得tag 56 int id = Convert.ToInt32(btn.GetTag(btn.Id)); 57 58 Intent it = new Android.Content.Intent(); 59 60 it.SetClass(context,typeof(Activity2)); 61 it.PutExtra("pos", id); 62 //注意!啟動新Activity 63 context.StartActivity(it); 64 } 65 } 66 }Adapter
在其他地方顯示資料
1 string PosValue = Intent.GetStringExtra("pos"); 2 3 4 Toast.MakeText(this, PosValue.ToString(), ToastLength.Long).Show();View Code
歡迎交流——小査 [email protected]