環境配置:
操作系統: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下,就出現了這個期待的文件:
復制到手機中,安裝后,開始得瑟吧!
host下載
源碼下載
from:https://www.cnblogs.com/madyina/archive/2015/07/23/4671708.html