我正在做一个简单的应用程序来使用用户输入进行计算,但我在使用 Android Math 类进行计算时遇到了麻烦。编译器告诉我
无法从 EditText 转换为加倍
为了使用atan
和pow
数学函数,我需要这样做。
我不确定的第二件事是如何在我editTexts
的 id 中显示计算:mark1 和 mark2。
我的 MainActivity 看起来像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting the variables to the xml id's and setting the click listener on the calc button
offsetLength = (EditText)findViewById(R.id.offLength);
offsetDepth = (EditText)findViewById(R.id.offDepth);
ductDepth = (EditText)findViewById(R.id.ductDepth);
calculate = (Button)findViewById(R.id.calc);
calculate.setOnClickListener((OnClickListener) this);
}
//called when button is clicked.
public void OnClick(View v)
{
//calculations here:
double tri1,tri2;
double marking1,marking2;
marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);
tri1 = (float)offsetDepth/(float)offsetLength;
tri2 = (float)ductDepth/Math.atan((float)tri1);
marking2 = ductDepth/Math.atan(float)(tri2);
//passing the calc results to my CalcResult activity.
Intent myIntent = new Intent(this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);
startActivity(myIntent);
Intent i = new Intent(this,CalcResult.class);
break;
}
}
这是我“尝试”将两个计算结果传递给它们的editTexts的 CalcResult 类:
public class CalcResult extends MainActivity
{
EditText res1,res2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
res1 = (EditText)findViewById(R.id.mark1);
res2 = (EditText)findViewById(R.id.mark2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
Double mark1 = bundle.double("marking1");
Double mark2 = bundle.double("marking2");
}
}