1

に複数の追加を追加したいIntent。1つはaを保持しdouble、もう1つは保持しlongます。これは可能ですか?

もしそうなら、私はそれをどのように行うのでしょうか、そしてどのように私はそれぞれのエキストラから情報を得るのですか?

4

4 に答える 4

10

あなたの心が望むだけ多くのエクストラを追加することができIntentます、それらはすべて単なるキーバリューデータです:

Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);

また、同じキー名を使用して取得できます。

String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
于 2012-07-12T22:16:14.120 に答える
1
intent.putExtra(@ExtraDoubleKey, @ExtraDoubleValue);
intent.putExtra(@ExtraLongKey, @ExtraLongValue);

ここで、@ ExtraDoubleKeyは、エクストラ(つまり、「価格」など)にアクセスするために使用する文字列であり、@ ExtraDoubleValueは、エクストラの値(渡したいdouble変数)です。@ExtraLongKeyと@ExtraLongValueについても同様です。

次に、次のアクティビティでエクストラにアクセスするには、次を使用できます。

double doubleValue = getIntent().getExtras().getDouble(@ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(@ExtraLongKey);

キー@ExtraDoubleKeyを使用してdoubleextraの値を取得します。

于 2012-07-12T22:19:53.753 に答える
1

https://stackoverflow.com/a/11461530/776075

Devunwired は正しいです。しかし、私が見る方法は、タイプごとに 1 つの値しか保持できないということです。1 つの文字列、1 つの int、1 つの double などのように..

2 つの文字列値を含めることはできません。または 2 つの整数。私はプログラムでこれを経験しており、1 つの文字列と 1 つのブール値を使用してそれを克服しました。

于 2013-02-21T12:08:12.550 に答える
0

Bundleこれを使用して、パラメーターとしてインテントに渡すことができます。

Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
于 2012-07-12T22:14:58.427 に答える