gpt4 book ai didi

android - 将字符串从一个方法传递给另一个android

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:15 25 4
gpt4 key购买 nike

我是 android 开发的新手,开始学习一些东西......我想知道如何实现一件我知道非常基本的事情,但我找不到用我对 Android 编程的实际知识来做的方法。现在已经尝试了几个小时,但还是没有结果!我在这里发现了很多关于 stack overflow 的问题,人们问同样的事情,我已经尝试了很多,但终究没有成功!

我写了下面的代码,有 2 个方法(vo2maxCalculo 和 shareIt),“share it”方法应该从 MSG1 变量中获取值并将这个值插入到“shareBody”字符串中,这样用户就可以通过电子邮件等。

我该怎么做?

非常感谢!


        public void vo2maxCalculo(View v) {
float minutos2;
double calculo2;

EditText minutos1 = (EditText) findViewById(R.id.minutos);
TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


minutos2 = Float.parseFloat(minutos1.getText().toString());

calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
String msg1 = String.format("%.2f", calculo2);
resultado5.setText(msg1);


}

public void shareIt(View v) {

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = String.format("The result is:", msg1);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

最佳答案

像这样在两个作用域之外创建一个变量

String msg1; //Like this
public void vo2maxCalculo(View v) {
float minutos2;
double calculo2;

EditText minutos1 = (EditText) findViewById(R.id.minutos);
TextView resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);


minutos2 = Float.parseFloat(minutos1.getText().toString());

calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
msg1 = String.format("%.2f", calculo2); //Set it in here like this
resultado5.setText(msg1);


}

public void shareIt(View v) {

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");

//Can still use it in here as long as the other method got called first
String shareBody = String.format("The result is: %s", msg1);

sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Final Result");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

关于android - 将字符串从一个方法传递给另一个android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27953128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com