从 Intent 中获取 Extras(变量)
Get Extras (variables) out of an Intent
我想在我的 java (android) 项目 main activity
中从另一个 class
获得三个 variables
。因此,我将它们放在 bundle
中,然后将其放入 Intent
中。这些变量来自我在第三个 class 中创建的 Object
,如下所示:
public class Transaction {
private float value;
private int transaction_Date;
private String description;
public Transaction(float value, int transaction_Date, String description){
super();
this.value = value;
this.transaction_Date = transaction_Date;
this.description = description;
}
public float getValue(){
return value;
}
public int getTransaction_Date(){
return transaction_Date;
}
public String getDescription(){
return description;
}
public void setValue(float value) {
this.value = value;
}
public void setTransaction_Date(int transaction_Date) {
this.transaction_Date = transaction_Date;
}
public void setDescription(String description) {
this.description = description;
}
这是我尝试将数据放入提到的 Intent
:
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
Bundle depositBundle = new Bundle();
depositBundle.putFloat("value", Float.parseFloat(depositInput.getText().toString()));
depositBundle.putInt("date", Integer.parseInt(depositInputDate.getText().toString()));
depositBundle.putString("description", depositInputNote.getText().toString());
depositIntent.putExtras(depositBundle);
startActivity(depositIntent);
}
这(当然)不起作用:
//populated transaction list
protected void populateTransactionList() {
Intent depositIntent = getIntent();
Transaction deposit = (Transaction) getIntent().getExtras();
}
那么我怎样才能从我的 Intent
中得到我的三个变量?
通过intent传递自定义对象的方式是实现类似于下面给出的Transactionclass的Parcelable接口。
使交易 class 像下面给出的片段一样可打包
import android.os.Parcel;
import android.os.Parcelable;
public class `Transaction` implements Parcelable {
private float value;
private int transaction_Date;
private String description;
public Transaction(float value, int transaction_Date, String description) {
super();
this.value = value;
this.transaction_Date = transaction_Date;
this.description = description;
}
public float getValue() {
return value;
}
public int getTransaction_Date() {
return transaction_Date;
}
public String getDescription() {
return description;
}
public void setValue(float value) {
this.value = value;
}
public void setTransaction_Date(int transaction_Date) {
this.transaction_Date = transaction_Date;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(this.value);
dest.writeInt(this.transaction_Date);
dest.writeString(this.description);
}
protected Transaction(Parcel in) {
this.value = in.readFloat();
this.transaction_Date = in.readInt();
this.description = in.readString();
}
public static final Parcelable.Creator<Transaction> CREATOR = new Parcelable.Creator<Transaction>() {
@Override
public Transaction createFromParcel(Parcel source) {
return new Transaction(source);
}
@Override
public Transaction[] newArray(int size) {
return new Transaction[size];
}
};
}
如下所示将 Parcelable 对象放入意图中
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(
Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
depositIntent.putExtra("data",deposit);
startActivity(depositIntent);
}
并接收下面给出的意图
//populated transaction list
protected void populateTransactionList() {
Intent depositIntent = getIntent();
Transaction deposit = depositIntent.getParcelableExtra("data");
}
Transaction deposit = (Transaction) getIntent().getExtras();
临时演员是Bundle
。你知道这一点,因为你有:
depositIntent.putExtras(depositBundle);
填写这些额外内容。
由于您在 Bundle
上调用了 putFloat("value", ...)
、putInt("date", ...)
和 putString("description", ...)
,因此您将使用关联的 getter 从基于这些的 extras 中检索值键。这可以通过以下方式完成:
直接在 getIntent()
返回的 Intent
上调用 getFloatExtra()
、getIntExtra()
和 getStringExtra()
,或
通过getIntent().getExtras()
获取extras的Bundle
,然后调用getFloat()
、getInt()
和getString()
Bundle
A Transaction
不能进入 extras,因为你不能将任意 Java 对象放入 extras。你可以做成Transaction
implement Parcelable
,这样你就可以直接把它放到extras里了。
我想在我的 java (android) 项目 main activity
中从另一个 class
获得三个 variables
。因此,我将它们放在 bundle
中,然后将其放入 Intent
中。这些变量来自我在第三个 class 中创建的 Object
,如下所示:
public class Transaction {
private float value;
private int transaction_Date;
private String description;
public Transaction(float value, int transaction_Date, String description){
super();
this.value = value;
this.transaction_Date = transaction_Date;
this.description = description;
}
public float getValue(){
return value;
}
public int getTransaction_Date(){
return transaction_Date;
}
public String getDescription(){
return description;
}
public void setValue(float value) {
this.value = value;
}
public void setTransaction_Date(int transaction_Date) {
this.transaction_Date = transaction_Date;
}
public void setDescription(String description) {
this.description = description;
}
这是我尝试将数据放入提到的 Intent
:
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
Bundle depositBundle = new Bundle();
depositBundle.putFloat("value", Float.parseFloat(depositInput.getText().toString()));
depositBundle.putInt("date", Integer.parseInt(depositInputDate.getText().toString()));
depositBundle.putString("description", depositInputNote.getText().toString());
depositIntent.putExtras(depositBundle);
startActivity(depositIntent);
}
这(当然)不起作用:
//populated transaction list
protected void populateTransactionList() {
Intent depositIntent = getIntent();
Transaction deposit = (Transaction) getIntent().getExtras();
}
那么我怎样才能从我的 Intent
中得到我的三个变量?
通过intent传递自定义对象的方式是实现类似于下面给出的Transactionclass的Parcelable接口。
使交易 class 像下面给出的片段一样可打包
import android.os.Parcel;
import android.os.Parcelable;
public class `Transaction` implements Parcelable {
private float value;
private int transaction_Date;
private String description;
public Transaction(float value, int transaction_Date, String description) {
super();
this.value = value;
this.transaction_Date = transaction_Date;
this.description = description;
}
public float getValue() {
return value;
}
public int getTransaction_Date() {
return transaction_Date;
}
public String getDescription() {
return description;
}
public void setValue(float value) {
this.value = value;
}
public void setTransaction_Date(int transaction_Date) {
this.transaction_Date = transaction_Date;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(this.value);
dest.writeInt(this.transaction_Date);
dest.writeString(this.description);
}
protected Transaction(Parcel in) {
this.value = in.readFloat();
this.transaction_Date = in.readInt();
this.description = in.readString();
}
public static final Parcelable.Creator<Transaction> CREATOR = new Parcelable.Creator<Transaction>() {
@Override
public Transaction createFromParcel(Parcel source) {
return new Transaction(source);
}
@Override
public Transaction[] newArray(int size) {
return new Transaction[size];
}
};
}
如下所示将 Parcelable 对象放入意图中
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(
Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
depositIntent.putExtra("data",deposit);
startActivity(depositIntent);
}
并接收下面给出的意图
//populated transaction list
protected void populateTransactionList() {
Intent depositIntent = getIntent();
Transaction deposit = depositIntent.getParcelableExtra("data");
}
Transaction deposit = (Transaction) getIntent().getExtras();
临时演员是Bundle
。你知道这一点,因为你有:
depositIntent.putExtras(depositBundle);
填写这些额外内容。
由于您在 Bundle
上调用了 putFloat("value", ...)
、putInt("date", ...)
和 putString("description", ...)
,因此您将使用关联的 getter 从基于这些的 extras 中检索值键。这可以通过以下方式完成:
直接在
getIntent()
返回的Intent
上调用getFloatExtra()
、getIntExtra()
和getStringExtra()
,或通过
getIntent().getExtras()
获取extras的Bundle
,然后调用getFloat()
、getInt()
和getString()
Bundle
A Transaction
不能进入 extras,因为你不能将任意 Java 对象放入 extras。你可以做成Transaction
implement Parcelable
,这样你就可以直接把它放到extras里了。