您的位置:首页 > 移动开发 > Android开发

MonoForAndroid的按钮单击事件及监听器的实现方式

2014-06-05 11:48 423 查看
方法一::MonoForAndroid默认的C#语法特点

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello" />
</LinearLayout>

MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace AndroidApplication39
{
[Activity(Label = "AndroidApplication39", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

}
}
}


方法二:

外部类作为监听器
获取控件的方式为

Button
button = FindViewById<Button>(Resource.Id.MyButton); 

Main.axml同方法一

MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
namespace AndroidApplication39
{
[Activity(Label = "AndroidApplication39", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private TextView mTextView01;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.SetOnClickListener(new ClickListener());
}
}
public class ClickListener : Java.Lang.Object, View.IOnClickListener
{
int count = 1;
public void OnClick(View v)
{
Button button = v.FindViewById<Button>(Resource.Id.MyButton);
button.Text = string.Format("{0} clicks!", count++);
}
}
}

方法三:

外部类作为监听器

获取控件方式为
 Button
button = (Button)FindViewById(Resource.Id.MyButton);

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
namespace AndroidApplication39
{
[Activity(Label = "AndroidApplication39", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private TextView mTextView01;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
But
4000
ton button = (Button)FindViewById(Resource.Id.MyButton);
button.SetOnClickListener(new ClickListener());
}
}
public class ClickListener : Java.Lang.Object, View.IOnClickListener
{
int count = 1;
public void OnClick(View v)
{
Button button = (Button)v.FindViewById(Resource.Id.MyButton);
button.Text = string.Format("{0} clicks!", count++);
}
}
}

方法4:
内部类作为监听器

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
namespace AndroidApplication39
{
[Activity(Label = "AndroidApplication39", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = (Button)FindViewById(Resource.Id.MyButton);
ClickListener listener = new ClickListener();
button.SetOnClickListener(listener);
}
class ClickListener : Java.Lang.Object, View.IOnClickListener
{
int count = 1;
public void OnClick(View v)
{
Button button = (Button)v.FindViewById(Resource.Id.MyButton);
button.Text = string.Format("{0} clicks!", count++);
}
}
}
}


方法5:

直接绑定到标签
Android还有一种更简单的绑定事件监听器的的方式,直接在界面布局文件中为指定标签绑定事件处理方法。
对于很多Android标签而言,它们都支持如onClick、onLongClick等属性,这种属性的属性值就是一个形如xxx

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
android:onClick="clickHandler" />
</LinearLayout>


MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
namespace AndroidApplication39
{
[Activity(Label = "AndroidApplication39", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
}
public void clickHandler(View v)
{
Button button = (Button)v.FindViewById(Resource.Id.MyButton);
button.Text = string.Format("{0} clicks!", count++);
}
}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: