ایجاد برنامه اندرویدی برای ارسال و بازیابی مقادیراز یک Activity به Activity دیگر
دوشنبه 7 تیر 1395در این مقاله قصد داریم با تکنولوژی جدید زامارین (Xamarin Android) ،برنامه ای اندرویدی برای ارسال و بازیابی مقادیراز یک Activity به Activity دیگر ایجاد کنیم.
مرحله 1: ویژوال استدیو را باز کرده و یک پروژه جدید از قسمت Templates، Visual C#، Androidو Blank App را انتخاب میکنیم. نام و محل ذخیره سازی آن را انتخاب میکنیم.
مرحله 2 : بعد ما نیاز به ایجاد صفحه دوم داریم ، بنابراین به Solution Explorer ، Project Name ، Resources، layout میرویم .پس از آن بر روی Add-> New Item راست کلیک میکنیم که پس از آن کادر محاوره ای جدیدی باز میشود.

مرحله 3 : Android Layout را انتخاب و نام آن را Secondpage.axml میگذاریم.
مرحله 4 : وپس از آن ما نیاز به اضافه کردن یک Activity بیشترداریم ، بنابراین دوباره به Solution Explorer ، و Project Name میرویم .و سپس روی آن راست کلیک کرده و Add-> New Item را انتخاب میکنیم که پس از آن کادر محاوره ای جدید باز میشود.
مرحله 5: از پنجره باز شده Activity را انتخاب و نام SecondActivity.cs را برای آن در نظر میگیریم.
مرحله 6 :برای نمایش صفحه Design به Solution Explorer-> Project Name->Resources->layout میرویم و روی Main.axml کلیک میکنیم.
مرحله 7 :از جعبه ابزار با Drag and Drop ، یک Button، TextViewو PlainTextدر صفحه می اندازیم.
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="صفحه اول"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtpassvalue" />
<Button
android:text="ارسال"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnsend" />
</LinearLayout>
مرحله 8 : در این مرحله :برای نمایش صفحه Design به Solution Explorer-> Project Name->Resources->layout میرویم و روی Secondpage.axml کلیک میکنیم.
مرحله 9 : از جعبه ابزار با Drag and Drop ، یک Button، TextView صفحه می اندازیم.

XML Code
<?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">
<Button
android:text="بازیابی مقدار"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btngetvalue" />
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtgetvalue" />
</LinearLayout>
مرحله10 : :برای باز شدن کدWindow به Solution Explorer-> Project Name میرویم و روی MainActivity.cs کلیک میکنیم،سپس کد زیر را وارد میکنیم.
C# Code
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace DataValuePass
{
[Activity(Label = "DataValuePass", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity: Activity
{
EditText txtdatapass;
Button btnsend;
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
btnsend = FindViewById < Button > (Resource.Id.btnsend);
txtdatapass = FindViewById < EditText > (Resource.Id.txtpassvalue);
btnsend.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
var intent = new Intent(this, typeof(SecondActivity));
intent.PutExtra("DATA_PASS", txtdatapass.Text); //DATA_PASS is Identify the Value Pass variable
this.StartActivity(intent);
}
}
}
مرحله 11 : :برای باز شدن کدWindow به Solution Explorer-> Project Name میرویم و روی SecondActivity.cs کلیک میکنیم،سپس کد زیر را وارد میکنیم.
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace DataValuePass
{
[Activity(Label = "SecondActivity")]
public class SecondActivity: Activity
{
Button btngetvalue;
TextView txtgevalue;
string txtvalues;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Secondpage);
txtvalues = Intent.GetStringExtra("DATA_PASS"); //Get The Value
btngetvalue = FindViewById < Button > (Resource.Id.btngetvalue);
txtgevalue = FindViewById < TextView > (Resource.Id.txtgetvalue);
btngetvalue.Click += Btngetvalue_Click;;
}
private void Btngetvalue_Click(object sender, EventArgs e)
{
txtgevalue.Text = txtvalues;
}
}
}
در برنامه F5 یا Build and Run فشار میدهیم.
اولین صفحه مقدارText Box "سلام خوش آمدید" را ارائه میدهد و با کلیک برروی دکمه ارسال به صفحه دوم میرود.
صفحه دوم مقدار Text Box "سلام خوش آمدید" را بازیابی و در TextView نمایش میدهد.
- Xamarin
- 2k بازدید
- 1 تشکر