我想在单击按钮时在另一个 Activity 中显示计算的详细信息。 如何实现这一目标。 我的第一个 Activity java 代码是
public void onClick(View v)
{
if(v.getId()==R.id.Button07)
{
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
startActivity(intent);
}
我想做的计算是
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = textview1.getText().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
我希望在按下提交按钮时在下一个 Activity 中显示结果“电视”。 我需要在哪里包括这个计算。还有什么进一步的步骤
非常感谢任何帮助 谢谢
最佳答案
在您的第一个 Activity 中:
public void onClick(View v) {
//Put your calculation code here
Bundle b = new Bundle();
b.putString("answer", youranswer);
//You could also use putInteger, whichever you prefer.
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
intent.putExtras(b);
startActivity(intent);
}
在您的第二个 Activity 中,在 onCreate 中放置以下内容:
Bundle b = getIntent().getExtras();
String answer = b.getString("answer");
答案是你的 key 。它用于标识您想从 bundle 中获得什么。使用唯一键意味着您可以根据需要将多个值传递给下一个 Activity 。
关于安卓 Intent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5830326/