首先建立2個空白的Activity
如下圖所示,可自動生成相應xml檔案
我這建立了 OneActivity 和 TwoActivity
主要控制元件:在 介面1 中寫一個按鈕用來跳轉到 介面2
寫一個EditText元件用來輸入需要傳遞的資料 和 傳遞資料按鈕
再寫一個TextView元件用來接收回傳的資料
程式碼展示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Activity.OneActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="介面1" />
<Button
android:id="@+id/one_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="oneToTwo"
android:text="開啟介面2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="傳遞資料給Two" />
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入資料" />
<Button
android:id="@+id/btn_one_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="傳輸資料到Two介面" />
<TextView
android:id="@+id/tv_one_show_Tow_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="回傳的資料" />
</LinearLayout>
介面展示
主要控制元件:寫一個 TextView 元件用來顯示介面1傳遞過來的資料
寫一個 EditText 元件用來輸入需要回傳的資料 和 回傳按鈕
程式碼展示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Activity.TwoActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="介面2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示one介面傳遞的資料" />
<TextView
android:id="@+id/tv_intent_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_Tow_to_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="回傳資料給one介面" />
<EditText
android:id="@+id/et_Tow_back_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入回傳資料" />
</LinearLayout>
介面展示
程式碼展示
package com.example.myapplication.Activity;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.myapplication.R;
public class OneActivity extends AppCompatActivity {
private EditText input;
private Button send;
private TextView show;
//xml中記得onClick
public void oneToTwo(View View) {
//建立一個Intent意圖物件
Intent intent = new Intent(this, TwoActivity.class);
//跳轉
startActivity(intent);
}
/**
* public void oneDateTOTwoActivity(View View){
* //獲取輸入資料
* EditText input=findViewById(R.id.et_input);
* String text=input.getText().toString();
* //建立意圖物件
* Intent intent=new Intent(this,TwoActivity.class);
* //儲存資料
* intent.putExtra("msg",text);
* //跳轉
* //startActivity(intent);
*/
private void init() {
input = findViewById(R.id.et_input);
send = findViewById(R.id.btn_one_send);
show = findViewById(R.id.tv_one_show_Tow_text);
//給傳輸資料到Two介面的
按鈕繫結事件
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取輸入資料
String text = input.getText().toString();
//建立意圖物件
Intent intent = new Intent(OneActivity.this, TwoActivity.class);
//儲存資料
Bundle bundle = new Bundle();
bundle.putString("text", text);
intent.putExtras(bundle);
//跳轉
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//處理回傳資料
if (requestCode == 1) {
if (resultCode == 2) {
//獲取回傳資料
String msg = data.getStringExtra("msg");
show.setText(msg);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
init();
}
}
程式碼展示
package com.example.myapplication.Activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.myapplication.R;
public class TwoActivity extends AppCompatActivity {
private TextView show;
private Button TowtoOne;
private EditText back;
private void init() {
show = findViewById(R.id.tv_intent_show);
TowtoOne = findViewById(R.id.btn_Tow_to_one);
back = findViewById(R.id.et_Tow_back_text);
//獲取意圖物件
Intent intent = getIntent();
//獲取資料顯示
String text = intent.getStringExtra("text");
show.setText(text);
//資料回傳
//繫結按鈕事件
TowtoOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//設定要回傳的資料
String msg = back.getText().toString();
Intent data = new Intent();
data.putExtra("msg", msg);
//設定到結果碼
setResult(2, data);
TwoActivity.this.finish();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
init();
}
}