歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用Visual Studio 2015開發Android 程序

使用Visual Studio 2015開發Android 程序

日期:2017/3/1 9:25:42   编辑:Linux編程

環境配置:

操作系統:win 7 64位

IDE:Visual Studio 2015

SDK:installer_r24.3.3-windows

安裝前提:

編輯hosts文件(在附件可下載)因為安裝過程中要聯網更新和注冊

安裝完成VS之後直接新建Android程序會提示:

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

Microsoft Visual Studio

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

值不能為 null。參數名: path1

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

確定

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

那是因為VS沒有配置android的SDK,接下來我們就設置。

第一步:更新android SDK

自行百度並安裝installer_r24.3.3-windows.exe,然後打開安裝路徑下的SDK Manager選擇一個安卓版本更新,比如4.1.2,可以根據需要將其他版本對勾去掉。

然後等待更新完畢:

然後打開AVD Manager創建一個虛擬機:

點擊右邊的Start啟動看看能不能起起來。

第二步:新建android項目:

然後會要求你登陸:

需要先注冊,然後登陸。

然後依次點開資源管理器,找到布局文件:

雙擊打開設計界面:

工具箱上面已經內置了很多控件:

這裡無所謂了,喜歡拖就拖,不喜歡就自己寫布局代碼,咱們完成一個登陸界面:

完整代碼如:

<?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:

將輸出控制台拉大:

以後在運行中如果奔潰,可以在這裡找到詳細信息。

在虛擬機中進入控制面板:

啟動它,輸入信息:

點擊登錄:

第三步:部署app

經過第二步大家可以在debug目錄下找到apk安裝文件:

然後一激動就復制到手機中,結果發現根本用不了。

原因是VS中開發的apk需要發布才能安裝使用,發布按鈕就在

目前是灰的,需要將調試模式改為release才可用:

然後會出現發布向導:

這裡您請隨意!

然後繼續:

記住上面的路徑,一會就在這裡找安裝用APK文件。

然後等黑屏閃2下,就出現了這個期待的文件:

復制到手機中,安裝後,開始得瑟吧!

源碼下載 百度網盤:http://pan.baidu.com/s/1sj7PBfZ

host

------------------------------------------分割線------------------------------------------

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2015年資料/8月/26日/使用Visual Studio 2015開發Android 程序/

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割線------------------------------------------

源碼下載

------------------------------------------分割線------------------------------------------

FTP地址:ftp://ftp1.linuxidc.com

用戶名:ftp1.linuxidc.com

密碼:www.linuxidc.com

在 2015年LinuxIDC.com\8月\使用Visual Studio 2015開發Android 程序

下載方法見 http://www.linuxidc.com/Linux/2013-10/91140.htm

------------------------------------------分割線------------------------------------------

Copyright © Linux教程網 All Rights Reserved