使用Visual Studio 2015开发Android 程序

原文:使用Visual Studio 2015开发Android 程序html

环境配置:android

操做系统:win 7 64位windows

IDE:Visual Studio 2015app

SDK:installer_r24.3.3-windowside

安装前提:工具

编辑hosts文件(在附件可下载)由于安装过程当中要联网更新和注册布局

安装完成VS以后直接新建android程序会提示:post

---------------------------url

Microsoft Visual Studiospa

---------------------------

值不能为 null。参数名: path1

---------------------------

肯定   

---------------------------

那是由于VS没有配置android的SDK,接下来咱们就设置。

第一步:更新android SDK

自行百度并安装installer_r24.3.3-windows.exe,而后打开安装路径下的SDK Manager选择一个安卓版本更新,好比4.1.2,能够根据须要将其余版本对勾去掉。

而后等待更新完毕:

wps52D1.tmp

而后打开AVD Manager建立一个虚拟机:

wps52E2.tmp

点击右边的Start启动看看能不能起起来。

第二步:新建android项目:

wps52F2.tmp

而后会要求你登录:

wps5303.tmp

须要先注册,而后登录。

而后依次点开资源管理器,找到布局文件:

wps5304.tmp

双击打开设计界面:

工具箱上面已经内置了不少控件:

wps5314.tmp

这里无所谓了,喜欢拖就拖,不喜欢就本身写布局代码,我们完成一个登录界面:

wps5325.tmp

完整代码如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_margin="5dip">
    <TextView
        android:id="@+id/form_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="初始用户名和密码都是123" />
    <LinearLayout
        android:id="@+id/layout_login_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5.0dip"
        android:layout_marginTop="10.0dip"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆名:" />
        <EditText
            android:id="@+id/txt_login_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15.0sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/login_pwd_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_login_name"
        android:layout_centerHorizontal="true"
        android:layout_margin="5.0dip"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/login_pass_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码:"
            android:textSize="15.0sp" />
        <EditText
            android:id="@+id/txt_login_pwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:textSize="15.0sp" />
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:onClick="btn_click"
        android:text="登录" />
</LinearLayout>

这些代码稍微一用力就能看明白。

打开MainActivity 编辑代码以下:

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,  form_title
    // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.btn_login);
            EditText txtLoginName = FindViewById<EditText>(Resource.Id.txt_login_name);
            EditText txtLoginPwd = FindViewById<EditText>(Resource.Id.txt_login_pwd);
            TextView txtMsg = FindViewById<TextView>(Resource.Id.form_title);
            button.Click += delegate {
                                        string loginName = txtLoginName.Text;
                                        string loginPwd = txtLoginPwd.Text;
                                        if (loginName == loginPwd&& loginName == "123")
                                           {
                                              txtMsg.Text = "登录成功!";
                                           }
                                    };
  }

含义很简单,就是找到控件,取值,赋值,控件的ID在布局中定义@+id/后面的就是。

智能提示不灵光,暂时忍忍吧。

而后启动,按F5,若是想查看详细信息或者运行中异常,请依次打开logcat:

wps5326.tmp

将输出控制台拉大:

wps5337.tmp

之后在运行中若是奔溃,能够在这里找到详细信息。

在虚拟机中进入控制面板:

wps5347.tmp

启动它,输入信息:

wps5348.tmp

点击登陆:

wps5359.tmp

第三步:部署app

通过第二步你们能够在debug目录下找到apk安装文件:

wps5369.tmp

而后一激动就复制到手机中,结果发现根本用不了。

缘由是VS中开发的apk须要发布才能安装使用,发布按钮就在

wps536A.tmp

目前是灰的,须要将调试模式改成release才可用:

wps537B.tmp

而后会出现发布向导:

wps537C.tmp

这里您请随意!

wps538D.tmp

而后继续:

wps539D.tmp

记住上面的路径,一会就在这里找安装用APK文件。

而后等黑屏闪2下,就出现了这个期待的文件:

wps53AE.tmp

复制到手机中,安装后,开始得瑟吧!

wps53BE.tmp

host下载

源码下载

相关文章
相关标签/搜索