zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    安卓使用spinner控件和pull解析实现全国省市县的三级联动(附上xml文件)
    安卓linearlayout布局的一个嵌套实例
    接口回调的例子和安卓中的接口回掉实例
    Android Studio 快捷键
    java比较器 之compareable 和comparato比较
    4.Gradle构建Spring Boot项目
    2.Gradle安装和常用命令
    1.Gradle基础介绍
    6.SpringBoot学习(六)——Spring Boot Banner自定义
    4.SpringBoot学习(四)——Spring Boot Validation校验及原理
  • 原文地址:https://www.cnblogs.com/laxknight/p/3224655.html
Copyright © 2011-2022 走看看