-2

重複の可能性:
Androidのアクティビティ間でデータを渡す

メインアクティビティから作成した別のアクティビティに文字列を渡そうとしています。どうすればいいですか?

4

3 に答える 3

1

情報をエクストラとしてバンドルに保存することをお勧めします。以下のコードをご覧ください

これはあなたの最初の活動になります

String customerName = "Bob";
Bundle b = new Bundle();
Intent myIntent = new Intent(v.getContext(),work.class);

b.putString("Name", customerName);  
myIntent.putExtras(b);
v.getContext().startActivity(myIntent);

次に、他の(2番目の)アクティビティの情報にアクセスするには、以下のコードを参照してください

Bundle b = getIntent().getExtras(); 
String name = b.getString("customerName");      
于 2012-08-18T10:03:56.870 に答える
0

これを試して

FirstActivity

Bundle bundle = new Bundle();
bundle.putString("Your_KEY", "YOUR_STRING_VALUE");

Intent newIntent = new Intent(FirstActivity.this.getApplicationContext(), SecondActivity.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

SecondActivity

Bundle bundle = SecondActivity.this.getIntent().getExtras();
String s = bundle.getString("Your_KEY");
于 2012-08-18T10:15:48.073 に答える
0

secondActivityで

public static String myName;

myName="ミゲル";

mainActivityで取得したい場合:

 String s1;

 s1= secondActivity.myName;
于 2012-08-18T10:29:23.430 に答える