在这里,我编写了一个代码,通过使用意图将数据从一个活动传递到另一个活动..如果我需要在我的代码中进行任何更正,请告诉我。
OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
nextIntent.putExtra("firstname", "Siva");
nextIntent.putExtra("Secondname", "Kumar");
startActivity(nextIntent);
Toast.makeText(getApplicationContext(),"SignIn Button Clicked", Toast.LENGTH_SHORT).show();
}
};
第二项活动:
OnClickListener backListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
Intent receivedIntent = getIntent();
Bundle bundleData = receivedIntent.getExtras();
bundleData.getString("firstname");
bundleData.getString("secondname");
startActivity(backIntent);
}
};
答案 0 :(得分:1)
你应该尝试这样
将数据传递给SecondActivity
Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
nextIntent.putExtra("firstname", "Siva");
nextIntent.putExtra("Secondname", "Kumar");
startActivity(nextIntent);
在SecondActivity中获取数据
Intent receivedIntent = getIntent();
String firstname=receivedIntent.getStringExtra("firstname");
String secondname=receivedIntent.getStringExtra("Secondname");
修改强>
SecondActivity.jaja
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent receivedIntent = getIntent();
String firstname=receivedIntent.getStringExtra("firstname");
String secondname=receivedIntent.getStringExtra("Secondname");
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.text2);
txtSecond.setText(secondname);
Button back=(Button)findViewById(R.id.backbutton);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
答案 1 :(得分:0)
使用一些String变量,如下所示,
OnClickListener backListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
Intent receivedIntent = getIntent();
String firstName = getIntent().getStringExtra("firstname");
String secondName = getIntent().getStringExtra("Secondname");
startActivity(backIntent);
}
};
现在你的两个String变量firstName和secondName包含你需要的信息。