使用Visual Studio 2015開發Android 程序


環境配置:

操作系統: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,可以根據需要將其他版本對勾去掉。

然后等待更新完畢:

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

源碼下載

from:https://www.cnblogs.com/madyina/archive/2015/07/23/4671708.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM