java翻译到mono C#实现系列(1) 重写返回键按下的事件

今天看到群里的朋友问怎么按下返回键的时候提示信息,百度了下,就参考网上一个java版示例做了.没啥技术含量,就权当丰富下mono for android的小代码.

直接在mono新建的APP上修改的.

写个MessageBox类,负责提示各种消息.

showTips方法用来提示信息.

public override bool OnKeyDown重写了OnKeyDown方法.有空的朋友可以做漂亮点,我就懒得弄了,能看懂就行了.本人菜鸟,如果写的不对,请斧正.谢谢.

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace AndroidApplication1
{
    [Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : 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++); };
        }
        public class MessageBox
        {
            public static void Show(Context ctx, string title, string message)
            {
                AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
                dlg.SetTitle(title);
                dlg.SetMessage(message);
                dlg.SetPositiveButton("确定", delegate { });
                dlg.Show();
            }
            public static void ShowErrorMessage(Context ctx, Exception ex)
            {
                Show(ctx, "错误", ex.Message);
            }
        }

        private void showTips()
        {

            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .SetTitle("退出程序,嘿嘿")
                    .SetMessage("是否退出程序,嘿嘿")
                    .SetPositiveButton("确定,嘿嘿", delegate { MessageBox.Show(this, "提示", "恭喜你点确定了,不退出"); }).SetNegativeButton("取消", delegate { MessageBox.Show(this, "提示", "恭喜你点取消"); }).Show();//不退出,如果要退出就增加Finish(); 
            //.SetPositiveButton("确定,嘿嘿", delegate { MessageBox.Show(this, "提示", "恭喜你点确定了,退出");Finish(); }).SetNegativeButton("取消", delegate { MessageBox.Show(this, "提示", "恭喜你点取消"); }).Show();
            #region //注释的代码是java版
            //        private void showTips() {
            //        AlertDialog alertDialog = new AlertDialog.Builder(this)
            //            .setTitle("退出程序").setMessage("是否退出程序")
            //            .setPositiveButton("确定", new DialogInterface.OnClickListener() 
            //            {
            //                public void onClick(DialogInterface dialog, int which) 
            //                {
            //                    finish();
            //                }
            //            }).setNegativeButton("取消",
            //            new DialogInterface.OnClickListener() 
            //            {
            //                public void onClick(DialogInterface dialog, int which) 
            //                {
            //                    return;
            //                }
            //            }).create(); // 创建对话框
            //    alertDialog.show(); // 显示对话框
            //}
            #endregion
        }
        public override bool OnKeyDown(Keycode keyCode, KeyEvent events)
        {
            if (keyCode == Keycode.Back)//判断按下的键是否返回键
            {
                showTips();
                return true;
            }
            else return base.OnKeyDown(keyCode, events);//如果不是返回键,则调用原OnKeyDown方法
            #region //注释的代码是java版
            //public boolean onKeyDown(int keyCode, KeyEvent event) 
            //{
            //    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
            //    {
            //        showTips();
            //        return false;
            //    }
            //    return super.onKeyDown(keyCode, event);
            //}
            #endregion
        }
    }
}

代码下载:http://www.400gb.com/file/26907128

原文地址:https://www.cnblogs.com/laxknight/p/3224655.html