0

我有一个应用程序,它需要一个字符串来计算一些算法。它工作正常。现在我试图从主要活动中调用此活动,因此主要活动需要用户输入来覆盖第二个活动中的字符串。我在主要活动中有一个可编辑字段。我无法将主要活动的值传递给第二个活动。我怎样才能做到这一点?我的主要活动代码如下所示

public void onClick(View view){
       switch (view.getid())

    case R.id.calculate:

    setname= insertname.getText.toString(); //insertname is the Editablefield
    Intent i = new Intent(this, themain.class);

    i.putExtra("Name",insertname); //
    startActivityForresult(i,1);

    }

protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    super.onactivityresult(requestcode resultcode data);
    switch (requestcode ){

    case 1:
    //return result form activity 2
    }
    }

// Code for second activity is something like this:

public void onClick(View view) {
    switch (view.getid()) {

    case R.id.calculate:
        break;
    /*
     * this bit of code takes a string and gets data for this user and
     * returns it in an array. I am trying to overwrite this string
     */

    }
}
4

2 回答 2

1

尝试使用

Intent i = new Intent(this, themain.class);

i.putExtra("Name",setname);  // replace here with setname
startActivityForresult(i,1);
于 2012-12-24T03:58:16.950 回答
0

you can send your string to activity2 from your activity as following:

String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("name",str);
startActivityForresult(i,1);

you can receive str in activity2 as following:

String name = this.getIntent().getStringExtra("name");
于 2012-12-24T04:00:01.787 回答