xamarin开发Android程序示例
前面已经使用xamarin+visual studio配置了Android开发环境,这一次,来测试一下使用visual studio开发Android程序。听上去还是蛮有趣的。
首先使用想到新建一个Android 程序。新建 ->“Blank App(android)”,向导会自动创建所需要文件列表。最终创建效果如下:
这里需要注意到的是layout文件夹下有个main.axml,相当于使用eclipse创建时候的xml文件,这里,向布局文件内拖入一些空间,与Winform程序很类似,最终得到布局文件
如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:minWidth="25px"android:minHeight="25px"><Buttonandroid:text="Test Toast"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btnToast" /><TextViewandroid:text="Large Text"android:textAppearance="?android:attr/textAppearanceLarge"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/textView1" /><TextViewandroid:text="Medium Text"android:textAppearance="?android:attr/textAppearanceMedium"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/textView2" /><TextViewandroid:text="Small Text"android:textAppearance="?android:attr/textAppearanceSmall"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/textView3" /><Switchandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/switch1" /><RadioGroupandroid:minWidth="25px"android:minHeight="25px"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/radioGroup1"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="RadioButton"android:id="@+id/radioButton1" /><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="RadioButton"android:id="@+id/radioButton2" /><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="RadioButton"android:id="@+id/radioButton3" /></RadioGroup>
</LinearLayout>
这里使用线性布局,看上去与eclipse创建的布局语法无异。然后是针对于布局文件上的按钮进行编程,由于创建了activity,自动继承后,会重写
activity的oncreate方法,并设置main为主布局对象。与源生的Android编程不同,java中使用事件监听的方式,而C#使用委托的方法来实现点击事件。
java监听示例:
mPlayButton =(Button)v.findViewById(R.id.hellomoon_playButton);mPlayButton.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {mPlayer.play(getActivity());}});
本次实例中,我们主要做一件事件,点击按钮,改变按钮的text,并弹出toast提示,最终的代码如下,已经测试通过:
using Android.App;
using Android.Widget;
using Android.OS;
using static Android.Views.View;namespace App1
{[Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]public class MainActivity : Activity{Button btnToast;protected override void OnCreate(Bundle bundle){base.OnCreate(bundle);// Set our view from the "main" layout resourceSetContentView (Resource.Layout.Main);btnToast = (Button)FindViewById(Resource.Id.btnToast);btnToast.Click += BtnToast_Click;}private void BtnToast_Click(object sender, System.EventArgs e){string str = "Button Clicked";btnToast.Text = "Clicked!";Toast.MakeText(ApplicationContext, str, ToastLength.Long).Show();}}
}
xamarin开发Android程序示例相关推荐
- 【Xamarin开发 Android 系列 5】 Xamarin 的破解
[Xamarin开发 Android 系列 5] Xamarin 的破解 原文:[Xamarin开发 Android 系列 5] Xamarin 的破解 有关这个话题,十分敏感,公司开发还是支持下商业 ...
- Eclipse开发Android程序如何在手机上运行
http://www.cnblogs.com/plwang1990/archive/2011/07/04/2097757.htm android开发不论是在真机上调试还是最终发布到真机上都非常简单,过 ...
- 小米手机(HM1SW)高通开发android程序全过程
小米手机(HM1SW)开发android程序全过程 修改历史: 2016年5月9日 -------- 整理文档 a.增加了手机基本信息. b.增加360手机助手连接说明 2016年2月26日 - ...
- xamarin开发android收集的一些常用工具
#xamarin开发android收集的一些常用工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. ...
- 安卓 c 语言开发环境搭建,用C++语言开发Android程序 配置开发环境
如果你是一个C++语言的死忠,你喜欢C++语言到了偏执的状态,如果不想学习Java语言,或者你很讨厌Java语言,如果你认为Java虚拟机的内存占用太多和执行低效.如果你过度关注Andoird程序的执 ...
- Go语言开发Android程序
Go 语言开发 Android 程序 转自:http://studygolang.com/articles/9620 环境配置 安装 Go 1.5 以上版本,具体安装步骤见官网 https://gol ...
- xamarin Android 截屏,xamarin开发android收集的一些工具
xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Vi ...
- C# 使用Xamarin开发Android应用程序
微软移动跨平台开发(构建面向 Android.iOS 和 Windows phone应用): https://docs.microsoft.com/zh-cn/visualstudio/cross-p ...
- 【Xamarin开发 Android 系列 4】 Android 基础知识
什么是Android? Android一词的本义指"机器人",同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件. ...
最新文章
- Windows Phone 7 网络编程之留言板应用
- 复习笔记13 字符流与字节流
- DDPG-强化学习算法
- 李宏毅机器学习课程6~~~深度学习入门
- 短网址缩短和还原综合源码
- mysql 左连接 和全连接_mysql左连接,右连接,内连,全连
- 云计算成了“晕技术”就对了
- R语言︱ 数据库SQL-R连接与SQL语句执行(RODBC、sqldf包)
- 博一结束后的一些反思 -- 该如何平衡科研与生活
- JDK源码阅读-CharSequence接口
- 用ajax请求豆瓣api,结合豆瓣Api v2.0实现Jsonp跨域
- java fp-growth 算法包_java 实现fpGrowth算法
- 成功粉碎北信源监控程序vrvedp_m.exe ,vrvrf_c64.exe,svchost.exe,vrvrf_c.exe
- phpstudy快速搭建网站步骤(手把手教你搭建)
- 十年股市投资回顾与反思
- 什么是一维表 什么是二维表
- 进化计算——进化规划(EP)
- Xilinx UCF约束语法一
- ue4 unreal4 自定义网格 绘制自定义网格 绘制面 (Plus)
- tipask火车头采集器文章和提问发布模块|非免登录