简体   繁体   English

无法将EditText值转换为双精度/整数并在Android中执行数学函数

[英]Trouble converting EditText value to a double/integer and doing math functions in Android

First off, I'm a beginner at this... so if what I'm doing is pretty simple to fix (or do in general), I apologize :) 首先,我是一个初学者...所以,如果我要解决的事情很容易解决(或一般而言),我深表歉意:)

I'm trying to take user inputs (laps [int], fuel used [double], and laps of a feature [int]) to create a simple fuel calculator for racers. 我正在尝试接受用户输入(圈[int],使用的燃油[double]和功能[int]的圈)来为赛车手创建一个简单的燃油计算器。 However, I'm having trouble getting the variables to recognize. 但是,我很难识别变量。 I keep getting errors about I'm unable to use one function in a certain class and things like that. 我不断收到关于无法在某个类中使用某个函数之类的错误,以及类似的信息。 I've got the XML fields assigned to only take those types of numbers, so now I'm working strictly in the java. 我已经将XML字段分配为仅采用这些类型的数字,所以现在我严格在Java中工作。

I had the thing working in the java compiler, but taking it to Android is a whole other process I'm learning. 我已经在Java编译器中工作了,但是将其带到Android是我正在学习的其他整个过程。

After calculating from the user inputs, I want the assigned textView to change to the proper answers. 根据用户输入进行计算后,我希望分配的textView更改为正确的答案。 I think that part is ok, but getting the values to mathematically-viable values is where I'm running into a problem. 我认为这是可以的,但是要使值达到数学上可行的值是我遇到的问题。

I've attached the entire code here (pardon the redundancy with comments, keeps me from looking back and forth for the formula haha). 我在此处附加了整个代码(请原谅带有注释的多余部分,以免让我来回寻找公式哈哈)。 Any help you can provide would be appreciated! 您能提供的任何帮助将不胜感激! I'll do my best to learn from it for the other math-related activities in this app. 我将尽力从中学习该应用程序中与数学相关的其他活动。


    package com.tomcat.carolina.learning;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.TextView;




    //double pracLaps, fuelUsed, featureLaps, textLPGValue, textFuelNeededValue;


    public class Fuelsimple extends Activity implements OnClickListener{

EditText fuelUsed, pracLaps, featureLaps;
TextView textLPGValue, textFuelNeededValue;
//efficiency = (pracLaps / fuelUsed);
//fuelNeeded = (featureLaps / efficiency);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fuelsimple);

pracLaps = (EditText) findViewById(R.id.pracLaps);
fuelUsed = (EditText) findViewById(R.id.fuelUsed);
featureLaps = (EditText) findViewById(R.id.featureLaps);
Button gen = (Button) findViewById(R.id.submit);
textLPGValue = (TextView) findViewById(R.id.textLPGvalue);
textFuelNeededValue = (TextView) findViewById(R.id.textFuelNeededValue);








gen.setOnClickListener(new View.OnClickListener() {     
    public void onClick(View v) {
        // TODO Auto-generated method stub



        textLPGValue.setText(getNextDecimal(pracLaps / fuelUsed));
        textFuelNeededValue.setText(getNextDecimal(featureLaps/(pracLaps / fuelUsed)));
    };
    });

I hope this is what you are looking for: 我希望这是您要寻找的:

pracLaps = (EditText) findViewById(R.id.pracLaps);
fuelUsed = (EditText) findViewById(R.id.fuelUsed);
featureLaps = (EditText) findViewById(R.id.featureLaps);

pracLapsVar = Double.parseDouble(pracLaps.getText().toString());
fuelUserVar = Double.parseDouble(fuelUsed.getText().toString());
featureLapsVar = Double.parseDouble(featureLaps.getText().toString());


efficiency = (pracLapsVar / fuelUsedVar);
fuelNeeded = (featureLapsVar / efficiency);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM