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

Làm cách nào tôi có thể làm cho các đối tượng tùy chỉnh của mình có thể thành Parcelable?

Ví dụ này giải thích cách tôi có thể làm cho các đối tượng tùy chỉnh của mình có thể thành Parcelable

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"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/parcleObject"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_alignParentTop = "true"
      android:layout_centerHorizontal = "true"
      android:layout_marginTop = "27dp"
      android:text = "Click here"/>
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng chế độ xem nút để hiển thị các giá trị đối tượng lô đất.

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

package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   parcleObject sample;
   @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.parcleObject).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            sample = new parcleObject("sairamkrishna Mammahe");
            Toast.makeText(MainActivity.this,sample.getName(),Toast.LENGTH_LONG).show();
         }
      });
   }
}

Bước 3 - Thêm mã sau vào src / parcleObject.java

package com.example.myapplication;
import android.os.Parcel;
import android.os.Parcelable;

class parcleObject implements Parcelable {
   private String name;
   protected parcleObject(Parcel in) {
      this.name = in.readString();
   }
   public parcleObject(String name){
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public static final Creator<parcleObject> CREATOR = new Creator<parcleObject>() {
      @Override
      public parcleObject createFromParcel(Parcel in) {
         return new parcleObject(in);
      }
      @Override
      public parcleObject[] newArray(int size) {
         return new parcleObject[size];
      }
   };
   @Override
   public int describeContents() {
      return 0;
   }
   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(this.name);
   }
}

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 tôi có thể làm cho các đối tượng tùy chỉnh của mình có thể thành Parcelable?

Bây giờ bấm vào nút, nó sẽ hiện ra kết quả như hình bên dưới -

Làm cách nào tôi có thể làm cho các đối tượng tùy chỉnh của mình có thể thành Parcelable?