Android 中Activity 之間傳遞參數(shù)
Android 中Activity 之間傳遞參數(shù)
1.傳遞簡單數(shù)據(jù)
在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,TheActivity.class);
// 對基礎(chǔ)的數(shù)據(jù)類型進(jìn)行傳遞
i.putExtra("data","我是國人");
startActivity(i);
}
});
在B Activity中接受數(shù)據(jù)
tv =(TextView)findViewById(R.id.TheTextView);
Intent i = getIntent();
tv.setText(i.getStringExtra("data"));
這種傳值就是傳遞基本的數(shù)據(jù)類型
2.傳遞數(shù)據(jù) 包Bundle
在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,TheActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name","qll");
bundle.putInt("age",3);
// i.putExtras(bundle);
// 另種傳遞方式
i.putExtra("data",bundle);
startActivity(i);
}
});
}
在B Activity中接受數(shù)據(jù)
tv =(TextView)findViewById(R.id.TheTextView);
editText = (EditText) findViewById(R.id.editText3);
Intent i = getIntent();
// Bundle date = i.getExtras();
// 接受方式不同
Bundle date = i.getBundleExtra("data");
tv.setText(String.format("name=%s,age=%d",date.getString("name"),date.getInt("age")));
這總傳遞方式類似iOS中傳遞字典數(shù)據(jù)類型過來 。
3.傳遞值對象
自定義一個User類,傳遞自定義類需要對類進(jìn)行序列化
用Serializable進(jìn)行序列化
這種方法只需要類實(shí)現(xiàn)Serializable接口就可以了
User 類
import java.io.Serializable;
public class User implements Serializable{
private String name;
private int age;
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public User(String name,int age){
this.name = name;
this.age = age;
}
在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,TheActivity.class);
i.putExtra("user",new User("qll",24));
startActivity(i);
}
});
}
在B Activity中
tv =(TextView)findViewById(R.id.TheTextView);
User user = (User)i.getSerializableExtra("user");
tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));
用Parcelable實(shí)現(xiàn)
同樣的需要實(shí)現(xiàn)Parcelable接口
User 類
package com.example.wyhaiapple.transferdata1;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.ParcelableSpan;
public class User implements Parcelable{
private String name;
private int age;
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public User(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getName());
dest.writeInt(getAge());
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source.readString(),source.readInt());
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
}
在A Activity中 與上面的相同
在B Activity中
tv =(TextView)findViewById(R.id.TheTextView);
User user = (User)i.getParcelableExtra("user");
tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));
4.獲取 Activity 的返回參數(shù)
在B Activity中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the);
tv =(TextView)findViewById(R.id.TheTextView);
editText = (EditText) findViewById(R.id.editText3);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("data",editText.getText().toString());
setResult(1,i);
finish();
}
});
}
}
在A Activity中
startActivityForResult(i,0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
textView.setText("返回的值:"+data.getStringExtra("data"));
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
RxJava 1升級到RxJava 2過程中踩過的一些“坑”
RxJava2相比RxJava1,它的改動還是很大的,那么下面這篇文章主要給大家總結(jié)了在RxJava 1升級到RxJava 2過程中踩過的一些“坑”,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下來要一起看看吧。2017-05-05
Android 混合動畫詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 混合動畫詳解及實(shí)現(xiàn)代碼的相關(guān)資料,簡單的一種動畫(如旋轉(zhuǎn)、縮放、漸變、位移等)有時候并不能滿足我們項(xiàng)目的要求,這時候就需要運(yùn)用到混合動畫,需要的朋友可以參考下2016-11-11
Android自定義View控件實(shí)現(xiàn)刷新效果
這篇文章主要介紹了Android自定義View控件實(shí)現(xiàn)刷新效果的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Android下2d物理引擎Box2d用法簡單實(shí)例
這篇文章主要介紹了Android下2d物理引擎Box2d用法,實(shí)例分析了在Android平臺上使用Box2d的基本技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
Flutter實(shí)現(xiàn)增強(qiáng)版的頁面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個懸浮在頂部的按鈕,這個按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來和大家詳細(xì)聊聊2023-01-01
Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時所見的群聊頭像,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

