如何将字符串从activity2发送到activity1?
我的 android 应用程序有一个小/大问题。我有 2 个活动:Activity1 和 Activity2。 Activity1 有一个 TextView 和一个 Button,Activity2 有一个 EditText 和一个 Button。从 Activity1 转到 Acvity2,我想将 EditText 中的文本从 Activity2 发送到 Activity 1。我强制关闭,因为我不知道代码中的顺序。我应该怎么办?谢谢
这是我的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity1 extends Activity {
TextView txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView) findViewById(R.id.txt);
Button next=(Button)findViewById(R.id.btn);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivityForResult(intent, 0);
}
});
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity2 extends Activity {
EditText txt2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
txt2 = (EditText)findViewById(R.id.txt2);
final Bundle bundle = new Bundle();
String x=txt2.getText().toString();
bundle.putString("param",x);
Button btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Activity2.this,Activity1.class);
intent.putExtra(bundle)
finish();
}
});
}
}
I have a little/big problem in my app in android.I have 2 activity:Activity1 and Activity2. Activity1 has a TextView and a Button and Activity2 has an EditText and a Button. From Activity1 I go to Acvity2 and I want to send the text from EditText from Activity2 to Activity 1. I get force close because I don't know the order in code. What should I do? Thanks
Here is my code :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity1 extends Activity {
TextView txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView) findViewById(R.id.txt);
Button next=(Button)findViewById(R.id.btn);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivityForResult(intent, 0);
}
});
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity2 extends Activity {
EditText txt2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
txt2 = (EditText)findViewById(R.id.txt2);
final Bundle bundle = new Bundle();
String x=txt2.getText().toString();
bundle.putString("param",x);
Button btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Activity2.this,Activity1.class);
intent.putExtra(bundle)
finish();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码的一个问题是您没有任何东西来处理第二个活动的结果。 (第一个 Activity 中没有 onActivityResult。)
记事本教程 包括调用第二个活动并获得结果的好例子。
One problem with your code is that you don't have anything handling the result of the second activity. (There's no onActivityResult in the first activity.)
The notepad tutorial includes a decent example of calling a second activity and getting a result.
在 Activity1 中 @override onActivityResult。
并在 Activity2 中写下此内容。
txt2 = (EditText)findViewById(R.id.txt2);
In Activity1 @override onActivityResult.
and In Activity2 write this.
txt2 = (EditText)findViewById(R.id.txt2);
在 Activity1 中 @override onActivityResult。
并在 Activity2 中写下此内容。
In Activity1 @override onActivityResult.
and In Activity2 write this.