VS2015下的Android開發系列02——用VS開發第一個Android APP


配置Android模擬器

  這算是第一篇漏下說的,配置好VS的各參數,新建Android項目后,會發現菜單下的工具欄會多出Android相關的工具欄,紅色圈出的就是AVD。

  打開AVD后可以從模版處選一個設備,然后自己再做細節參數調整。

  然后選擇要模擬的版本,因為APP有藍牙BLE的支持需求,所以選擇了至少API Level18,注意如果安裝了HAXM,CPU/ABI項一定要選"Intel Atom (x86)",如果沒有,說明組件未安裝,趕緊去下載后再來;另外一個注意點是內存至少3G,要不運行起來卡成狗,不要問我是怎么知道的……

  運行效果

建立一個Android APP

  通過新建項目對話框,新建一個空的Android項目

 

   新建后的項目基本目錄如下(里頭的layout1.axml和Activity1.cs是后面加的)

AndroidManifest.xml

  首選第一個要說的是AndroidManifest.xml,這是APP中非常重要的一部分,但在VS中不用像JAVA的開發環境中一樣把相關配置寫全,打開只看到基本的部分,其余的VS會在編譯時補全。AndroidManifest.xml中的內容基本可以在項目屬性中配置完成

(namespace、API Level等的配置)

(APP版本、名稱、權限等)

(這里還沒實際經驗,保持默認,等搞清楚了再來說)

 

 Assets和Resource資源目錄

  Assets里放的是二進制資源文件,如字體、聲音,訪問方式如下(具體參考Assets目錄下的AboutAssets.txt文件里面有示例代碼):

//Any raw assets you want to be deployed with your application can be placed in
//this directory (and child directories) and given a Build Action of "AndroidAsset".
//
//These files will be deployed with you package and will be accessible using Android's
//AssetManager, like this:

public class ReadAsset : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        InputStream input = Assets.Open ("my_asset.txt");
    }
}

//Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");

 

  Resource目錄中包含了布局、圖片、字符串字典等資源文件,同樣來看下資源的調用說明(目錄中的AboutResources.txt):

/*
Images, layout descriptions, binary blobs and string dictionaries can be included 
in your application as resource files.  Various Android APIs are designed to 
operate on the resource IDs instead of dealing with images, strings or binary blobs 
directly.

For example, a sample Android app that contains a user interface layout (main.axml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) 
would keep its resources in the "Resources" directory of the application:

Resources/
    drawable/
        icon.png

    layout/
        main.axml

    values/
        strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource".  The native Android APIs do not operate directly with filenames, but 
instead operate on resource IDs.  When you compile an Android application that uses resources, 
the build system will package the resources for distribution and generate a class called "R" 
(this is an Android convention) that contains the tokens for each one of the resources 
included. For example, for the above Resources layout, this is what the R class would expose:
*/

public class R {
    public class drawable {
        public const int icon = 0x123;
    }

    public class layout {
        public const int main = 0x456;
    }

    public class strings {
        public const int first_string = 0xabc;
        public const int second_string = 0xbcd;
    }
}

/*
You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main 
to reference the layout/main.axml file, or R.strings.first_string to reference the first 
string in the dictionary file values/strings.xml.
*/

 

 Activity代碼

  除了這些目錄以外,別的就是Activity的代碼及自己的類代碼啦,APP的默認啟動Activity只要設備類的相關屬性就OK了,不需要特定的名字,重點注意:“MainLauncher = true, Icon = "@drawable/icon"”。至於別的代碼是不是和JAVA的基本一樣一樣的?

[Activity(Label = "演示APP_01", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        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,
            // and attach an event to it

        }

    }

如點擊按鈕啟動另外一個Activity的代碼,有沒有熟悉的感覺?

Button btnOpenActivity = FindViewById<Button>(Resource.Id.btnOpenActivity);

btnOpenActivity.Click += (sender, e) =>
            {
                Intent intent = new Intent(this, typeof(Activity1));
                StartActivity(intent);
            };

為Activity添加兩個按鈕和一些簡單的代碼后調試運行效果圖:

 


免責聲明!

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



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