简体   繁体   English

如何在Android中将两个字符串从一个活动传递到另一个活动

[英]How to pass two Strings from one Activity to another Activity in Android

I know how to pass one string from one activity to another, but how would you do this for two strings "nameString" and "addressString"? 我知道如何将一个字符串从一项活动传递到另一项活动,但是您如何对两个字符串“ nameString”和“ 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);
        }
    });

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 将字符串检索到MapsActivity

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

Hope it will help you :) 希望它能对您有所帮助:)

Similar the way u passed 1st string u can pass 'n' number of strings via intent. 与您传递第一个字符串的方式类似,您可以通过意图传递“ n”个字符串。

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

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将两个字符串从一个活动传递到另一个活动 - pass two strings from one activity to another 如何将多个记录从一个活动传递到android中的另一个活动? - How to pass multiple records from one activity to another activity in android? 无法同时将两个字符串从一个活动传递到另一个活动 - Cannot pass two strings from one activity to another activity at same time 将字符串从一个活动传递到另一个活动,Android - Pass String from one Activity to another activity, android 如何在Android中将用户输入从一项活动传递到另一项活动 - How to pass user input from one activity to another in Android MVP Android:如何将数据从一个活动传递到另一个活动? - MVP Android: how to pass data from one activity to another? 如何在Android Studio中将堆栈从一个活动传递到另一个活动 - How to pass a stack from one activity to another in android studio 如何在Android的同一活动中将日期从一个片段传递到另一个片段? - How to pass date from one fragment to another in the same activity in Android? 如何将BluetoothAdapter从一个活动传递到Android中的另一个活动? - How to pass a BluetoothAdapter from one activity to another in Android? 如何在Android上将对象从一个活动传递到另一个活动 - How to pass an object from one activity to another on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM