Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để chuyển một đối tượng từ Hoạt động này sang Hoạt động khác trong Android?

Ví dụ này giải thích cách tôi chuyển một đối tượng từ Activity này sang Activity khác trong android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Click here to pass object to Second Activity!"
      android:id="@+id/button"
      android:layout_centerInParent="true" />
</RelativeLayout>

Bước 3 - Tạo một lớp java và thêm mã sau vào Character.java

import java.io.Serializable;
public class Character implements Serializable {
   String name, Proffession, Position;
   String[] abilities;
   public Character(String name, String proffession, String position, String[] abilities) {
      this.name = name;
      Proffession = proffession;
      Position = position;
      this.abilities = abilities;
   }
}

Bước 4 - Thêm mã sau vào MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
            Character sports = new Character("CR7", "Football", "Left Winger", new String[]{"Best Freeckicker in the World"});
            intent.putExtra("Character", sports);
            startActivity(intent);
         }
      });
   }
}

Bước 5 - Tạo một hoạt động emty, đặt tên là SecondActivity và thêm mã sau vào SecondActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Arrays;
public class SecondActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      Character sports = (Character)getIntent().getSerializableExtra("Character");
      TextView textView = findViewById(R.id.textView);
      textView.setText(sports.name +"\n" + sports.Proffession + "\n" + sports.Position+ "\n" + Arrays.toString(sports.abilities));
   }
}

Bước 6 - Thêm mã sau vào activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".SecondActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:textSize="20sp"
      android:layout_centerInParent="true"/>
</RelativeLayout>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để chuyển một đối tượng từ Hoạt động này sang Hoạt động khác trong Android?

Làm cách nào để chuyển một đối tượng từ Hoạt động này sang Hoạt động khác trong Android?