2011-03-30 37 views
9

我有兩個Intent s和兩個Activity s。從意圖傳遞數據到其他意圖

我在第一個IntentEditText

我想用在EditText文本在第二意圖和傳遞給第二個意圖

Intent myIntent = new Intent(mycurentActivity.this, secondActivity.class); 
startActivity(myIntent); 

預先感謝您

回答

12

你找Intent#putExtra(String, String).

下面是一個例子:

Intent myIntent = new Intent(mycurentActivity.this, secondActivity.class); 
myIntent.putExtra("key", myEditText.Text.toString(); 
startActivity(myIntent); 

當你收到我ntent你可以再次提取它:

String text = myIntent.getStringExtra("key"); 

(http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String))

+1

我把代碼就像你寫的第二個活動,然後Eclipse詢問我創造一個局部變量意向並對其進行初始化爲空; 然後我運行代碼,我得到一個空屏幕 在此先感謝。 最好的regrads – User616263 2011-03-30 22:11:36

+1

你應該創建一個局部變量int。請顯示你的代碼。 – RoflcoptrException 2011-03-30 22:12:43

+0

有沒有辦法傳遞對象?這種方式非常迂迴 – Vincent 2011-04-01 09:07:14

0

第一項活動

Intent myIntent = new Intent(rechercheCP.this, XMLParsing.class); 
        myIntent.putExtra("key", autoComplete.getText().toString()); 
        startActivity(myIntent); 

在第一活動第二個活動

TextView a; 
String text = myIntent.getStringExtra("key"); 
a = new TextView(this); 
    a.setText(text); 
    layout.addView(a); 
0

//... 
final static int EDIT=0; 
//...(action trigger) 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    Intent intent; 
    intent = new Intent().setClass(mycurentActivity.this, secondActivity.class); 
    startActivityForResult(intent, EDIT); 
} 
//... 

,後來在第一個活動

//... 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    switch(requestCode){ 
     case EDIT: 
      if(resultCode == RESULT_OK){ 
      String text = data.getStringExtra("key"); 
      //do whatever with the text... 
     }else if(resultCode == RESULT_CANCELED){ 
     } 
    break; 
    } 
} 
//... 

//... 
Intent intent = new Intent().setClass(secondActivity.this, mycurentActivity.class); 
intent.putExtra("key", myEditText.getText().toString); 
setResult(RESULT_OK, intent); 
finish(); 
//...