我将先前开发的Java小程序转移到Android应用程序时遇到了一些困难。我使用Eclipse作为IDE。两种语言之间似乎存在微妙的细微差别和差异。
它是一个计算器程序,允许用户从两个微调器中选择材料类型和形状类型。如果他们选择矩形,则直径文本字段应该切换为禁用。如果他们选择圆柱体,那么高度和宽度字段应该切换为禁用。然后计算按钮进行数学运算,清除按钮重置所有内容。 Java applet就像一个魅力,所以我不确定这里的问题是什么。非常感谢帮助!
以下是该代码最重要的摘录:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.text.DecimalFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import org.****.materialcalculator.R;
public class MaterialCalculator extends ActionBarActivity {
public Spinner materialDD;
public Spinner shapeDD;
DecimalFormat d = new DecimalFormat("#");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_material_calculator);
materialDD = (Spinner) findViewById(R.id.materialDD);
shapeDD = (Spinner) findViewById(R.id.shapeDD);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
final EditText txtDiameter = (EditText) findViewById(R.id.txtDiameter);
final EditText txtLength = (EditText) findViewById(R.id.txtLength);
final EditText txtWidth = (EditText) findViewById(R.id.txtWidth);
final EditText txtHeight = (EditText) findViewById(R.id.txtHeight);
final EditText edtTxtTotprice = (EditText) findViewById(R.id.edtTxtTotprice);
final EditText edtTxtWeight = (EditText) findViewById(R.id.edtTxtWeight);
btnCalculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strmaterialDD = materialDD.getSelectedItem().toString();
String strshapeDD = shapeDD.getSelectedItem().toString();
double number1 = Double.parseDouble(
txtDiameter.getText().toString());
double number2 = Double.parseDouble(
txtLength.getText().toString());
double number3 = Double.parseDouble(
txtWidth.getText().toString());
double number4 = Double.parseDouble(
txtHeight.getText().toString());
//If 'Rectangle' is selected:
if(strshapeDD.equals("Rectangle")) {
//6061
if(strmaterialDD.equals("Aluminum 6061")) {
edtTxtTotprice.setText("Total Price: $"
+d.format((0.098*(number2*number3*number4)*3.06)));}
//2024
if(strmaterialDD.equals("Aluminum 2024")) {
edtTxtTotprice.setText("Total Price: $"
+d.format((0.1*(number2*number3*number4)*5.61)));}
//7075
if(strmaterialDD.equals("Aluminum 7075")) {
edtTxtTotprice.setText("Total Price: $"
+d.format((0.101*(number2*number3*number4)*7.85)));
//If 'Cylinder' is selected:
} else if(strshapeDD.equals("Cylinder")) {
//6063
if(strmaterialDD.equals("Aluminum 6063")){
edtTxtTotprice.setText("Total Price: $"
+d.format((0.098*(((number1/2)*(number1/2))*3.14159*number2))*3.06));}
//2024
if(strmaterialDD.equals("Aluminum 2024")){
edtTxtTotprice.setText("Total Price: $"
+d.format((0.1*(((number1/2)*(number1/2))*3.14159*number2))*5.61));}
//7075
if(strmaterialDD.equals("Aluminum 7075")) {
edtTxtTotprice.setText("Total Price: $"
+d.format((0.101*(((number1/2)*(number1/2))*3.14159*number2))*7.85));}
//Mic-6
if(strmaterialDD.equals("Mic-6")) {
edtTxtTotprice.setText("Total Price: $"
+d.format((0.097544*(((number1/2)*(number1/2))*3.14159*number2))*2.28));}
//17-4
if(strmaterialDD.equals("17-4")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.282*(((number1/2)*(number1/2))*3.14159*number2))));}
//1018
if(strmaterialDD.equals("Steel 1018")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.2843*(((number1/2)*(number1/2))*3.14159*number2))));}
//4140
if(strmaterialDD.equals("Steel 4140")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.282*(((number1/2)*(number1/2))*3.14159*number2))));}
//OI
if(strmaterialDD.equals("OI")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.282*(((number1/2)*(number1/2))*3.14159*number2))));}
//Grade 2
if(strmaterialDD.equals("Titanium Grade 2")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.163*(((number1/2)*(number1/2))*3.14159*number2))));}
//B360
if(strmaterialDD.equals("Brass 360")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.307*(((number1/2)*(number1/2))*3.14159*number2))));}
//Acetal
if(strmaterialDD.equals("Acetal")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.0564*(((number1/2)*(number1/2))*3.14159*number2))));}
//CPVC
if(strmaterialDD.equals("CPVC")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.05635*(((number1/2)*(number1/2))*3.14159*number2))));}
//PVC
if(strmaterialDD.equals("PVC")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.0564*(((number1/2)*(number1/2))*3.14159*number2))));}
//UHMW
if(strmaterialDD.equals("UHMW")) {
edtTxtTotprice.setText("Weight (lb):"
+d.format((0.0564*(((number1/2)*(number1/2))*3.14159*number2))));}
}}}}});
我认为,这部分是我问题的一部分。在我的Java程序中,我能够使用ActionPerformed
事件告诉程序要启用哪些字段。这是基于所选的形状。截至目前,它什么也没做,并且启用了所有字段。这可能导致我收到的大多数错误。
if (shapeDD.equals("Rectangle"))
txtDiameter.setText("0");
if (shapeDD.equals("Rectangle"))
txtDiameter.setEnabled(false);
if (shapeDD.equals("Cylinder"))
txtWidth.setEnabled(false);
if (shapeDD.equals("Cylinder"))
txtWidth.setText("0");
if (shapeDD.equals("Cylinder"))
txtHeight.setEnabled(false);
if (shapeDD.equals("Cylinder"))
txtHeight.setEnabled(false);
btnClearAll.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
edtTxtTotprice.setText("Total Price: $");
edtTxtWeight.setText("Weight (lb):");
txtDiameter.setText("");
txtLength.setText("");
txtWidth.setText("");
txtHeight.setText("");
materialDD.equals("Aluminum 6063");
shapeDD.equals("Rectangle");
}});
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.material_calculator, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当我通过模拟器运行时,我会在{<计算'之后的NumberFormatException invalid double: ""
的LogCat中收到错误。单击按钮。这似乎表明一个字段在它不应该是空的时候是空的,但我相信错误可能在于上面的代码。
在Eclipse中,parseDouble是一个可接受的方法吗?
如何在没有onActionPerformed
的情况下使shapeDD微调器声明工作?
提前致谢!