Android:从TextView获取String并将其传递给其他几个活动

时间:2016-08-18 21:51:00

标签: java android

我正在尝试将少量字符串从活动1的TextView传递给活动2以进行添加操作。这是成功的。

现在,是否可以通过使用活动2中的按钮执行减法操作,将现有字符串从活动1传递到活动3?

这是我的活动2,添加操作

Intent intent = getIntent();

String string1 = intent.getStringExtra("no1");
String string2 = intent.getStringExtra("no2");

double addition = Double.parseDouble(string1) + Double.parseDouble(string2);
answer.setText(String.valueOf(addition));

这是我的活动2,将传递字符串的按钮

public void onClick(View v) {
if (v.getId() == R.id.btnPage2) {        

Bundle extras = getIntent().getExtras();  
String string1 = extras.getString("no1");
String string2= extras.getString("no2");

Intent intent = new Intent(getApplicationContext(),Act3.class);
startActivity(intent); 

这是我的活动3,减法操作

    Intent intent = getIntent();

    String string1 = intent.getStringExtra("no1");
    String string2 = intent.getStringExtra("no2");

    double subtract = Double.parseDouble(string1) - Double.parseDouble(string2);
    answer.setText(String.valueOf(subtract));

我对如何从其他活动中获取字符串有一些问题。请帮我弄清楚从其他几个活动中获取字符串输入的正确方法。

谢谢!

2 个答案:

答案 0 :(得分:1)

假设活动2具有来自活动1的有效Intent(带有额外内容),那么您需要在活动2 putExtras(Bundle)中运行活动3的Intent。

例如,在活动2中,将传递字符串的按钮:

public void onClick(View v) {
    if (v.getId() == R.id.btnPage2) {        
         Intent intent = new Intent(getApplicationContext(), Act3.class);
         intent.putExtras(getIntent().getExtras());   

         startActivity(intent);
    }
} 

答案 1 :(得分:0)

添加

intent.putExtra("no1", string1);
intent.putExtra("no2", string2);

Intent intent = new Intent(getApplicationContext(),Act3.class);
活动中的

2.将字符串添加到活动3的意图中。