Building Android Apps with Xamarin: An Example Code Walkthrough
a simple example of a Xamarin Android app that displays "Hello World" in a TextView:
In MainActivity.cs:
using Android.App;
using Android.Widget;
using Android.OS;
namespace HelloWorld
{
[Activity(Label = "Hello World", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get the TextView from the layout resource
TextView helloTextView = FindViewById<TextView>(Resource.Id.helloTextView);
// Set the text of the TextView
helloTextView.Text = "Hello World!";
}
}
}
In Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/helloTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
This app sets the content view to the "main" layout resource, finds the TextView with the ID "helloTextView", and sets its text to "Hello World!". You can customize this app by adding more views, handling user input, and using more Android APIs.
Komentar
Posting Komentar