我是Android开发的新手,并开始学习一些东西...... 我想知道如何实现一个我知道非常基本的东西,但我找不到一种方法来实现我对Android编程的实际知识。已经尝试了几个小时,毕竟没有结果!我发现很多关于堆栈溢出的问题,人们问同样的事情,我已经尝试了很多,但毕竟没有成功!
我用2种方法(vo2maxCalculo和shareIt)编写了以下代码,"分享它"方法应该从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"));
}
答案 0 :(得分:2)
在这样的范围内创建一个变量
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"));
}
答案 1 :(得分:1)
如果我理解正确,那么这就是解决方案。我也相信几乎所有的编程语言都是这样做的。
这两种方法似乎都是点击侦听器:仅当它们不是时才使用
public String vo2maxCalculo(View v) { // changed here
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);
return msg1; // added this
}
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:", vo2maxCalculo(v)); // Why String.format???
String shareBody = "The result is:" + vo2maxCalculo(v); // Replace this
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 = ""; // Added 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); // Edited this
resultado5.setText(msg1);
}
其他方法没有变化
答案 2 :(得分:0)
试试这个
移动widgets
的{{1}}中所有onCreate(...)
的所有初始化
Activity
您可以创建一个名为minutos1 = (EditText) findViewById(R.id.minutos);//declare it globally at class level
resultado5 = (TextView) findViewById(R.id.vo2max12Resultado);//declare it globally at class level
的单独类,并在Utils类中创建一个静态方法进行计算,并在Utils
事件中调用它(通过这种方式,您可以从任何类调用此方法)< / p>
onClick
这些按钮的public static String calculate(float minutos2) {
double calculo2 = 33 + 0.17 * (minutos2 - 1955) / 15;
String msg1 = String.format("%.2f", calculo2);
return msg1;
}
中的就是这样的
onClick
答案 3 :(得分:0)
propper wat是使用全局变量(如前所述)并在调用后更新vo2maxCalculo中的变量。
另一种方法 - 您可以使用`resultado5.getText()。toString();
从resultado5中提取文本你没有提到,但你可能会不成功地实施onClick行为?在这种情况下,添加布局xml文件android:onClick="vo2maxCalculo"
和android:onClick="shareIt"
如果这些方法在其他活动中,则应在创建活动时传递intent或在singleTon中保存数据(仅对大型havie单个对象(如位图)有用)
答案 4 :(得分:0)
最后,在您的宝贵帮助下,所有问题都解决了,工作代码是:
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"));
}
感谢大家花了很多时间帮助我解决问题!