1

我正在做一个简单的应用程序来使用用户输入进行计算,但我在使用 Android Math 类进行计算时遇到了麻烦。编译器告诉我

无法从 EditText 转换为加倍

为了使用atanpow数学函数,我需要这样做。

我不确定的第二件事是如何在我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");
    }

}
4

1 回答 1

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(); 

现在根据您的要求将字符串转换为浮点数的两倍

 double off = Double.parseDouble(getoffsetlength);
于 2013-09-25T15:27:16.077 回答