توی این آموزش می خوایم اطلاعات وارد شده در یک ادیت تکست رو با زدن روی یک دکمه به اکتیویتی دیگر ارسال کرده و آنرا توسط یک تکست ویوو نمایش دهیم سپس توی اکتیویتی دوم با زدن روی دکمه برگشت اطلاعاتی رو به اکتیویتی دوم ارسال کنیم و با یه پیغام نمایش بدهیم.
ابتدا لایه های xml رو طراحی می کنیم :
لایه مربوط به MainActivity:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shadow.hadi.passdata.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Data To Activity Second" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="Enter Data" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="62dp"/> </RelativeLayout>
لایه مربوط به SecondActivity:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shadow.hadi.passdata.SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go Back" android:id="@+id/button2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="74dp"/> </RelativeLayout>
کدهای مربوط به فایل جاوا MainActivity :
public class MainActivity extends AppCompatActivity { //تعریف private Button btnSend; private EditText edtText; static final int REQUST_CODE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // وصل کردن به لایه xml btnSend = (Button) findViewById(R.id.button); edtText = (EditText) findViewById(R.id.editText); // وقتی روی دکمه کلیک شد btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // گرفتن مقدار داخل ادیت تکست و گذاشتن داخل متغییر String data = edtText.getText().toString(); // اگر مقداری وارد شده بود if (!data.isEmpty()){ // تعریف اینتنت برای فرستادم اطلاعات Intent i = new Intent(MainActivity.this,SecondActivity.class); i.putExtra("Key",data); startActivityForResult(i,REQUST_CODE); }else { // نمایش پیغام درصورت وارد نکردن اطلاعات Toast.makeText(getApplicationContext(),"Put Data",Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUST_CODE){ if (resultCode == RESULT_OK){ // گرفتن اطلاعات برگشتی از اکتیویتی دوم String result = data.getStringExtra("resultBack"); // نمایش Toast.makeText(this,result,Toast.LENGTH_SHORT).show(); } } } }
کدهای مربوط به فایل جاوا SecondActivity :
public class SecondActivity extends AppCompatActivity { private TextView txtData; private Button btnBack; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //وصل کردن به لایه txtData = (TextView) findViewById(R.id.textView); btnBack = (Button) findViewById(R.id.button2); // تعریف باندل Bundle b = getIntent().getExtras(); if (b != null) { // گرفتن اطلاعات فرستاده شده به و سیله کلید String data = b.getString("Key"); // مقدار دهی به تکست ویوو txtData.setText(data); } // وقتی رو دکمه کلیک شد btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // تعریف اینتنت Intent resultData = getIntent(); resultData.putExtra("resultBack", "From SecondActivity"); resultData.putExtra("somthingElse", "some thing else"); setResult(RESULT_OK, resultData); finish(); } }); } }
نمای کلی پروژه :
لینک دانلود سورس پروژه : دانلود
written article. I will be sure to bookmark it and come back to learn extra of your helpful information. Thanks for the post.
I will certainly return.