A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
Hello,
Welcome to our Microsoft Q&A platform!
If you want to create a Xamarin.Android application, then you want to ask user's name in the xaml. Then get the user name 's value in the C# code?
Firstly, you can create an Xamarin.Android project. Select a blank application.
Then open the activity_main.xml in the Resource folder-> layout folder.
Put following code to activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"/>
<Button
android:layout_below="@id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="get name"/>
</RelativeLayout>
Then, open the MainActivity.cs, use findviewbyId to get these two controls(edittext and button) in the OnCreate method like following code.
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);
EditText editText1 = FindViewById<EditText>(Resource.Id.editText1);
Button button1 = FindViewById<Button>(Resource.Id.button1);
button1.Click += (o, e) =>
{
//we can get the user name herer
var UserName=editText1.Text;
//display this username in a Toast for testing.
Toast.MakeText(this, UserName, ToastLength.Short).Show();
};
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.