我有一个应用程序,它采用字符串来计算一些算法。它工作正常。 现在我尝试从主活动调用此活动,因此主活动采用用户输入来覆盖第二个活动中的字符串。我在主活动中有一个可编辑字段。我无法将值从主活动传递到第二个。我怎样才能做到这一点?我的主要活动代码如下所示
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
*/
}
}
答案 0 :(得分:1)
尝试使用
Intent i = new Intent(this, themain.class);
i.putExtra("Name",setname); // replace here with setname
startActivityForresult(i,1);
答案 1 :(得分:0)
您可以将自己的字符串发送到活动中的activity2,如下所示:
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("name",str);
startActivityForresult(i,1);
你可以在activity2中接收str,如下所示:
String name = this.getIntent().getStringExtra("name");