mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context – 查海涅

需求:为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”
5 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″?>
<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 }

 

Item类

class Item
{
//标题 textview
public string title { get; set; }
//按钮
public string button { get; set; }
}

 

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 }

在其他地方显示数据

1 string PosValue = Intent.GetStringExtra(pos);
2
3
4 Toast.MakeText(this, PosValue.ToString(), ToastLength.Long).Show();

 欢迎交流——小査 [email protected]

 

欢迎大家访问我老婆的淘宝店……说是博客园看到的包邮哦!   

http://yishids.taobao.com

 

 

 

 

本文链接:http://www.cnblogs.com/zhahainie/p/3573528.html,转载请注明。



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量