天天看點

vs2019開發android應用,VS 2019開發APP(一)界面和代碼

1.界面

在Resources>layout>目錄下的*.xml檔案就是界面檔案

vs2019開發android應用,VS 2019開發APP(一)界面和代碼

2.關聯界面

接下來,通過将支援代碼插入到 MainActivity 類中來添加代碼以關聯使用者界面。

在 MainActivity 類中找到 OnCreate 方法,在其中添加關聯按鈕代碼如下:

protected override void OnCreate(Bundle savedInstanceState)

{

base.OnCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

// Set our view from the "main" layout resource

SetContentView(Resource.Layout.activity_main);

//通過ID綁定控件

Button Task1 = FindViewById(Resource.Id.button1);

Button Task2 = FindViewById(Resource.Id.button2);

Button Task3 = FindViewById(Resource.Id.button3);

Button Task4 = FindViewById(Resource.Id.button4);

Button Stop= FindViewById(Resource.Id.button5);

TextView Show= FindViewById(Resource.Id.textView1);

//添加控件對應的事件

Task1.Click += (sender, e) =>

{

Show.Text = "任務一進行中……";

};

Task2.Click += (sender, e) =>

{

Show.Text = "任務二進行中……";

};

Task3.Click += (sender, e) =>

{

Show.Text = "任務三進行中……";

};

Task4.Click += (sender, e) =>

{

Show.Text = "任務四進行中……";

};

}

現在就完成了一個簡單的響應按鈕事件的程式了。