Android——舉例詳解在Activity之間交換數據的Bundle

什麼是Bundle ?

  • 鍵值對的組合

在這裏插入圖片描述

bundle.putCharSequence("str1",str1);
intent.putExtras(bundle);
startActivity(intent);

模擬淘寶的填寫並顯示收貨地址的功能。

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

MainActivity:

public class MainActivity extends AppCompatActivity {

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str1=((EditText)findViewById(R.id.text1)).getText().toString();
                String str2=((EditText)findViewById(R.id.text2)).getText().toString();
                String str3=((EditText)findViewById(R.id.text3)).getText().toString();
                String str4=((EditText)findViewById(R.id.text4)).getText().toString();
                if(!str1.equals("")&&!str2.equals("")&&!str3.equals("")&&!str4.equals("")){
                    Intent intent=new Intent(MainActivity.this,AddressActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putCharSequence("str1",str1);
                    bundle.putCharSequence("str2",str2);
                    bundle.putCharSequence("str3",str3);
                    bundle.putCharSequence("str4",str4);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this,"請填寫完整",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

AddressActivity:

public class AddressActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String str1=bundle.getString("str1");
        String str2=bundle.getString("str2");
        String str3=bundle.getString("str3");
        String str4=bundle.getString("str4");
        TextView textView1=(TextView)findViewById(R.id.view1);
        TextView textView2=(TextView)findViewById(R.id.view2);
        TextView textView3=(TextView)findViewById(R.id.view3);
        TextView textView4=(TextView)findViewById(R.id.view4);
        textView1.setText(str1);
        textView2.setText(str2);
        textView3.setText(str3);
        textView4.setText(str4);
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請填寫以下信息:"
        android:textSize="20dp"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view"
        android:id="@+id/text1"
        android:hint="請輸入姓名"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:id="@+id/text2"
        android:hint="請輸入手機號"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text2"
        android:id="@+id/text3"
        android:hint="請輸入收貨地址"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text3"
        android:id="@+id/text4"
        android:hint="請輸入郵編"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="提交"
        android:layout_below="@id/text4"
        android:background="#564563"
        android:textColor="#ff9564"
        />
</RelativeLayout>

activity_address:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view1"
        android:textSize="50dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view2"
        android:textSize="50dp"
        android:layout_below="@+id/view1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view3"
        android:textSize="50dp"
        android:layout_below="@+id/view2"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view4"
        android:textSize="50dp"
        android:layout_below="@+id/view3"
        />

</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章