Android:如何使用Intent发送多个变量?

时间:2016-12-18 19:22:45

标签: java android android-intent android-studio-2.2

我从main.java发送了一些变量:

    Bundle bund = new Bundle();
    Intent intent = new Intent(this, newWindow.class);
    String name = editName.getText().toString();
    bund.putString(yourName, name);
    String pass= editPassword.getText().toString();
    bund.putString(yourPass,pass);
    String mail=EditMail.getText().toString();
    bund.putString(yourMail,mail);
    intent.putExtras(bund);
    startActivity(intent);

newWindow.java

        Intent intent = getIntent();
        Bundle extras =  intent.getExtras();
           String name = extras.getString(main.yourName);
           String mail = extras.getString(main.yourMail);
           String pass = extras.getString(main.yourPass);
        viewText1 = (TextView) findViewById(R.id.textView2);
        viewText2 = (TextView) findViewById(R.id.textView3);
        viewText3 = (TextView) findViewById(R.id.textView4);
        viewText1.setText(name);
        viewText2.setText(mail);
        viewText3.setText(pass);

问题是,只有最后一个变量被传递并显示在所有3个textview的

2 个答案:

答案 0 :(得分:4)

据推测,您在所有情况下都使用相同的密钥。换句话说,您yourName的值与yourPass的值相同,后者与yourMail的值相同。这些需要具有独特的价值。通常,我们会为static final String使用Bundle个键。

答案 1 :(得分:0)

为什么不避开Bundle部分?

只需在FirstActivity中,试试这个

Intent intent = new Intent(this, NewWindow.class);
String name = editName.getText().toString();
String pass= editPassword.getText().toString();
String mail=EditMail.getText().toString();
intent.putExtra("name",name);
intent.putExtra("mail",mail);
intent.putExtra("pass",pass);
startActivity(intent);

然后在RecievingActivity中执行此操作

Intent extras=getIntent();
String name = extras.getStringExtra("name");
String mail = extras.getStringExtra("mail");
String pass = extras.getStringExtra("pass");
viewText1.setText(name);
viewText2.setText(mail);
viewText3.setText(pass);

如果有帮助请告诉我