Intent傳參數


Intent 是Android 程序中各組件之間進行交互的一種重要方式,它不僅可以指明當前組
件想要執行的動作,還可以在不同組件之間傳遞數據。Intent 一般可被用於啟動活動、啟動
服務、以及發送廣播等場景


   // A activity調用    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);  // 創建視圖
        setContentView(R.layout.my_layout);
        // 找到對應的button來監聽事件
        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AnotherAty.class);
                i.putExtra("data", "hello word");  // 使用Intent來傳參
                startActivity(i);
            }
        });
        System.out.println("onCreate");
    }
    //B activity 通過Intent來獲取值,並顯示在textView上面
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);

        Intent i = getIntent();  //直接獲取傳過來的intent
        tv = (TextView)findViewById(R.id.textView);
        tv.setText(i.getStringExtra("data"));
    }

 

 

// 如果數據比較多,可以通過 Bundle  數據包來傳遞數據

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);  // 創建視圖
        setContentView(R.layout.my_layout);
        // 找到對應的button來監聽事件
        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AnotherAty.class);
                //i.putExtra("data", "hello word");  // 使用Intent來傳參
                Bundle b = new Bundle();  // 打包數據
                b.putString("name", "chengzhier");
                b.putInt("age", 2);

                i.putExtras(b);
                startActivity(i);
            }
        });
        System.out.println("onCreate");
    }


private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);

        Intent i = getIntent();  //直接獲取傳過來的intent
        tv = (TextView)findViewById(R.id.textView);

        //i.getStringExtra("data")
        Bundle data = i.getExtras();
        String s = String.format("name=%s, age=%d", data.getString("name"), data.getInt("age"));
        tv.setText(s);
    }

 

 

// 傳遞一個對象 java 自帶的 Serializable 虛擬化  

 
        
// User類
public class User implements Serializable{  //讓這個對象序列化
    private String name;
    private int age;

    public User(int age, String name ) {
        this.age = age;
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

// A activity
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);  // 創建視圖
        setContentView(R.layout.my_layout);
        // 找到對應的button來監聽事件
        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AnotherAty.class);

                i.putExtra("user", new User(2, "zh"));

                startActivity(i);
            }
        });
        System.out.println("onCreate");
    }


// B activiry
 private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);

        Intent i = getIntent();  //直接獲取傳過來的intent
        tv = (TextView)findViewById(R.id.textView);
        User u = (User)i.getSerializableExtra("user");  //取出來
         
        String s = String.format("測試11name=%s, age=%d", u.getName(), u.getAge());
        tv.setText(s);
    }

 

 

//傳遞一個對象 安卓專門的Parcelable虛擬化  

/**
 * Created by ZhouXiaoHai on 2016/9/8.
    User 類
 */
public class User implements Parcelable{  // 安卓自帶的序列化
    private int age;
    private String name;
    private String dogName;



    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public String getDogName() {
        return dogName;
    }

    public User(int age, String name, String dogName) {
        this.age = age;
        this.name = name;
        this.dogName = dogName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {  // 必須要寫的 接口 Parcelable 的方法
        // 這個一定要按變量順序寫
        dest.writeInt(getAge());
        dest.writeString(getName());
        dest.writeString(getDogName());
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            // 這個一定要按變量順序寫
            return new User( source.readInt(), source.readString(), source.readString());
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
}


//A activity
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);  // 創建視圖
        setContentView(R.layout.my_layout);
        // 找到對應的button來監聽事件
        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AnotherAty.class);

                i.putExtra("user", new User(2, "zh", "旺財"));

                startActivity(i);
            }
        });
        System.out.println("onCreate");
    }


//B activity
private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);

        Intent i = getIntent();  //直接獲取傳過來的intent
        tv = (TextView)findViewById(R.id.textView);
        //User u = (User)i.getSerializableExtra("user");
        User u = (User)i.getParcelableExtra("user");

        String s = String.format("測試21name=%s, age=%d 狗的名字=%s", u.getName(), u.getAge(), u.getDogName());
        tv.setText(s);
    }

 

簡單總結(小白): Serializable 比 Parcelable 使用起來方便,直接實現接口就好了,但是效率不高。  Parcelable效率高,但是需要自己寫一些代碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM