首页 技术 正文
技术 2022年11月9日
0 收藏 688 点赞 4,590 浏览 2524 个字

今天看到群里的朋友问怎么按下返回键的时候提示信息,百度了下,就参考网上一个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 = ; 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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,157
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,625
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,470
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,241
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,878
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,045