1. 程式人生 > >xamarin android如何監聽單擊事件

xamarin android如何監聽單擊事件

在xamarin android單擊事件是最基礎的事情,看過菜鳥上的android教程時,java寫的都是監聽事件,為一個按鈕,單選按鈕、多選按鈕的單擊事件有三種,前面兩種用的非常普遍,也很簡易,我這裡主要就是寫一下xamarin android中的監聽事件。

1.使用委託:

button.Click += delegate {

  button.Text = string.Format (“{0} clicks!”, count++);

};

2:使用Lamda表示式 :

button.Click += (s, e) =>{ 

     button.Text = string.Format (“{0} clicks!”, count++);

};

3.Xamarin android單選按鈕監聽事件:

namespace App914
{
    [Activity(Label = "App914", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity,IOnCheckedChangeListener
    {
        int count = 1;

        /// <summary>
        /// 實現OnCheckedChangeListener的介面
        /// </summary>
        /// <param name="group"></param>
        /// <param name="checkedId"></param>
        public void OnCheckedChanged(RadioGroup group, int checkedId)
        {
            RadioButton rdBtn = (RadioButton)FindViewById(checkedId);
            Toast.MakeText(this, rdBtn.Text, ToastLength.Short).Show();
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            RadioGroup rg = FindViewById<RadioGroup>(Resource.Id.rg);
            rg.SetOnCheckedChangeListener(this);
        }
    }
}

佈局檔案我就不貼了,注意1.使用RadioGroup包含兩個或者多個RadioButton ,,注意RadioGroup,RadioButton每個ID都必須要寫上2.實現RadioGroup單擊事件的介面
IOnCheckedChangeListener

4.Xamarin android按鈕監聽事件:



namespace App914
{
    [Activity(Label = "App914", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity,View.IOnClickListener
    {
        int count = 1;
        public void OnClick(View v)
        {
            Button btn = FindViewById<Button>(Resource.Id.MyButton);
            btn.Text =string.Format( "實現xamarin android單擊監聽事件{0}",count++);
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.SetOnClickListener(this);
        }
    }
}
普通按鈕實現單擊事件的監聽,注意介面是IOnClickListener

5.Xamarin android按鈕監聽事件:

namespace App914
{
    [Activity(Label = "App914", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity, CompoundButton.IOnCheckedChangeListener
    {
        public void OnCheckedChanged(CompoundButton compoutButton,Boolean b)
        {
            if (compoutButton.Checked)
            {
                Toast.MakeText(this,compoutButton.Text.ToString(),ToastLength.Long).Show();
            }
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            CheckBox cb_1 = (CheckBox)FindViewById(Resource.Id.cb_one);
            CheckBox cb_2 = (CheckBox)FindViewById(Resource.Id.cb_two);
            CheckBox cb_3 = (CheckBox)FindViewById(Resource.Id.cb_three);

            cb_1.SetOnCheckedChangeListener(this);
            cb_2.SetOnCheckedChangeListener(this);
            cb_3.SetOnCheckedChangeListener(this);
        }
    }
}


多選按鈕的監聽事件佈局檔案我就不貼出來,同樣是要注意的幾點和RadioButton的監聽事件一樣

總結:

雖然說在xamarin中事件的監聽用的不多,和lamda、delegate比起來也不方便,但是非常有必要了解一下,新手學xamarin的時候監聽還是很有必要學一下的,不要的話參考菜鳥上的android教程不易理解,畢竟java android中就是用的監聽,同時要注意的是每個元素的監聽事件所實現的介面不一樣,這是要注意的一點