我正在做一个简单的应用程序来使用用户输入进行计算但是我在使用Android Math类进行计算时遇到了麻烦。编译器告诉我
无法从EditText转换为double
我需要做的是使用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;
}
}
这是CalcResult类,我“尝试”将两个计算结果传递给它们的editTexts:
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");
}
}
答案 0 :(得分:0)
你有这个
double marking1,marking2; // double
marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);
Intent myIntent = new Intent(this, CalcResult.class);
myIntent.putExtra("number1", marking1);
检索时你有这个
String mark1 = bundle.getString("marking1"); // retrieving as string
更改为
double mark1 = bundle.getDouble("marking1"); // similar for mark2
<强>更新强>
你有这个
offsetLength = (EditText)findViewById(R.id.offLength);
然后
marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2);
您甚至没有从editext获取值。
点击
String getoffsetlength = offsetLength.getText().toString();
现在根据您的要求将字符串转换为float的两倍
double off = Double.parseDouble(getoffsetlength);