How to pass two Strings from one Activity to anoth

2019-09-14 12:58发布

I know how to pass one string from one activity to another, but how would you do this for two strings "nameString" and "addressString"?

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addressString = address.getText().toString();
            nameString = name.getText().toString();
            Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
            intent.putExtra("address", addressString);
            AddLocationActivity.this.startActivity(intent);
        }
    });

3条回答
欢心
2楼-- · 2019-09-14 13:20

this way you can pass and retrive string from one activity to another activity

Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
intent.putExtra("address", addressString);
intent.putExtra("string2", string2);
startActivity(intent);

retrive String to MapsActivity

Intent intent = getIntent();
String address = intent.getStringExtra("address");
String string2 intent.getStringExtra("string2");

Hope it will help you :)

查看更多
疯言疯语
3楼-- · 2019-09-14 13:21

Similar the way u passed 1st string u can pass 'n' number of strings via intent.

Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
intent.putExtra("address", addressString);
intent.putExtra("string2", string2);
AddLocationActivity.this.startActivity(intent);
查看更多
Fickle 薄情
4楼-- · 2019-09-14 13:24

To pass:

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addressString = address.getText().toString();
            nameString = name.getText().toString();
            Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
            intent.putExtra("name",nameString);
            intent.putExtra("address", addressString);
            AddLocationActivity.this.startActivity(intent);
        }
    });
}

To get:

Intent intent = getIntent();
String address = intent.getStringExtra("name");
String string2 intent.getStringExtra("address");
查看更多
登录 后发表回答