一、Bundle類
Bundl類是為字串與某組件物件建立映射關系的組件,它與Intent配合使用,可在不同的Activity之間傳送資料,常用方法如下:
1、putString:把字串用鍵值對的形式放到Bundle物件中,
2、remove:移除指定key的值,
3、getString:獲取指定key的值,
二、應用Intent在不同的Activity之間傳送資料
1、在界面的Activity A端
(1)在界面的Activity物件和Bundle物件,
Intent intent=new Intent();
Bundle bundle=new Bundle();
(2)為Intent指定要跳轉的界面,并用Bundle存放鍵值對資料,
Intent.setClass(MainActivity.thiw.secondActivity.class);
bundle.putString("標記1","要傳送的資訊內容");
2、在另一界面的Activity B端
(1)從Intent中獲取Bundle物件,
Bundle bb=this.getIntent().getString().getExras();
(2)從Bundle物件中按鍵值對的鍵名獲取對應的資料值,
String str=rb.getString("標記1");
三、在不同的Avtivity界面之間傳遞資料
1、實作效果如下:


2、布局檔案activity_main.xml的源代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<!-- tools:context=".MainActivity">-->
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這里是MainActivity"
android:textSize="36sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="72dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.074" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳轉到SecondActivity"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="134dp" />
<EditText
android:id="@+id/edit"
android:layout_width="220dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="請輸入一個詞語"
android:textSize="24sp"
android:layout_marginTop="41dp"
app:layout_constraintTop_toBottomOf="@+id/txt2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、布局檔案activity_second.xml的源代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<!-- tools:context=".MainActivity">-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="歡迎來到 SecondActivity"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.3" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="248dp"
android:layout_marginLeft="8dp"
android:text="回傳第一個頁面"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4、控制檔案MainActivity.java的源代碼如下:
package com.example.myapplication2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
Button btn;
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("edit", edit.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
5、控制檔案SecondActivity.java的源代碼如下:
package com.example.myapplication2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
Button btn;
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("edit", edit.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/272020.html
標籤:其他
