11

メイン アクティビティには、値が設定されたいくつかの変数が含まれます。メインアクティビティからのデータを入力する必要があるフォームでサブアクティビティを作成したので、開始時にデータをサブアクティビティに渡す必要があると思います。

メインアクティビティからサブアクティビティに変数値を渡す方法を知っている人はいますか?

ありがとう!

4

3 に答える 3

20

このメソッドは、メイン アクティビティで使用できます。

Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);

サブアクティビティで終了し、通常は onCreate イベントで、このメソッドで値を取得します

int value = getIntent().getExtras().getInt("key");

これが役立つことを願っています。

于 2009-07-02T08:27:06.163 に答える
2

これはメインアクティビティで機能しますか?

Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);

に続く:

String value = getIntent().getExtras().getString("key");

そして、複数の「おまけ」などを追加できますか?

i.putExtra("key", value1); 
i.putExtra("key2", value2);
i.putExtra("key3", value3);

ありがとう...

于 2012-02-28T21:56:18.343 に答える
0

これを試してみてください:

activity1.class:

Intent i = new Intent(activity1.this,activity2.class);

Bundle b = new Bundle();
b.putString("name", "your value need to pass here");

i.putExtras(b);
startActivity(i);

activity2.class:

Bundle b = this.getIntent().getExtras();

String name = b.getString("name");

((TextView)findViewById(R.id.textView1)).setText(name);
于 2015-12-17T05:34:05.493 に答える