使用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下載

源碼下載

相關文章
相關標籤/搜索