简体   繁体   English

将数据从意图传递到另一个意图

[英]pass data from intent to an other intent

I have two Intent s and two Activity s. 我有两个Intent和两个Activity

I have in the first Intent an EditText . 我在第一个Intent一个EditText

I want to use the text in the EditText in the second intent and pass to the second intent 我想在第二个intent中使用EditText中的文本并传递给第二个intent

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

Thank you in advance 先感谢您

Your looking for Intent#putExtra(String, String). 你正在寻找Intent#putExtra(String,String)。

Here is an example: 这是一个例子:

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

When your receiving the Intent you can extract it again: 当您收到意图时,您可以再次提取它:

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

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

in the First activity 在第一项活动中

//...
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);
}
//...

and later in the First activity 以及后来的第一项活动

//...
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;
    }
}
//...

and second activity 和第二项活动

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

First Activity 第一项活动

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

Second Activity 第二项活动

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM