標籤:
作業系統: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下,就出現了這個期待的檔案:
複製到手機中,安裝後,開始得瑟吧!
使用Visual Studio 2015開發Android 程式