Java 如何获取两个EditText字段的值并执行简单的数学运算

Java 如何获取两个EditText字段的值并执行简单的数学运算,java,android,Java,Android,我是Android开发的新手,正在做一个简单的实践项目,用户在两个EditText字段中输入一个数字,然后按下一个标有“计算”的按钮,两个数字的总和就会显示出来。这是我到目前为止的代码,但我不知道如何添加两个字符串值并将其输出到名为“answer”的TextView字段: 要获取字符串的整数值,请执行以下操作: final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2); answer.setTe

我是Android开发的新手,正在做一个简单的实践项目,用户在两个EditText字段中输入一个数字,然后按下一个标有“计算”的按钮,两个数字的总和就会显示出来。这是我到目前为止的代码,但我不知道如何添加两个字符串值并将其输出到名为“answer”的TextView字段:


要获取字符串的整数值,请执行以下操作:

final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2);
answer.setText(Integer.toString(myIntResult));
然后可以执行加法并将结果存储在变量中。 然后将结果显示为字符串:

final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2);
answer.setText(Integer.toString(myIntResult));

要获取字符串的整数值,请执行以下操作:

final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2);
answer.setText(Integer.toString(myIntResult));
然后可以执行加法并将结果存储在变量中。 然后将结果显示为字符串:

final int myResult = Integer.parseInt(myString1) + Integer.parseInt(myString2);
answer.setText(Integer.toString(myIntResult));

您必须将它们转换为数字(int、float、long等)来执行您的算术。然后将结果转换回字符串以显示在TextView中

int val1 = Integer.parseInt(value);
int val2 = Integer.parseInt(value2);

answer.setText(String.valueOf(val1 + val2));

您必须将它们转换为数字(int、float、long等)来执行您的算术。然后将结果转换回字符串以显示在TextView中

int val1 = Integer.parseInt(value);
int val2 = Integer.parseInt(value2);

answer.setText(String.valueOf(val1 + val2));

把绳子拉长或者你需要的任何东西

public void calNumbers(View view) {
    EditText text = (EditText)findViewById(R.id.edit_number1);
    String value = text.getText().toString();       

    EditText text2 = (EditText)findViewById(R.id.edit_number2);
    String value2 = text2.getText().toString();              

    TextView answer = (TextView) findViewById(R.id.answer);      
    long l1 = Long.parseLong(text);
    long l2 = Long.parseLong(text2);

    long result = l1 + l2;
    answer.setText(Long.toString(result));
}

把绳子拉长或者你需要的任何东西

public void calNumbers(View view) {
    EditText text = (EditText)findViewById(R.id.edit_number1);
    String value = text.getText().toString();       

    EditText text2 = (EditText)findViewById(R.id.edit_number2);
    String value2 = text2.getText().toString();              

    TextView answer = (TextView) findViewById(R.id.answer);      
    long l1 = Long.parseLong(text);
    long l2 = Long.parseLong(text2);

    long result = l1 + l2;
    answer.setText(Long.toString(result));
}

您可以通过多种方式从EditText获取值,但是如果您想做的是确保操作首先确保这些。在编辑文本的XML布局中,确保
android:inputType
设置为
“number”
“numberSigned”
“numberDecimal”
,这样用户就不会干扰我们的应用程序

现在,在java类上,根据预期的输出,将数字的参数全局初始化为0,可以是整数(int)表示整数之间的和或减(inputType number或numberSigned),也可以是更一般的双精度表示所有操作

最后,您需要做的是解析信息,而不是将其作为文本字符串获取,因为它涉及两个不必要的转换

public void wattSystemView(View view) {

/**
* decimal values entered in hor_editText and wh_editText
* number of days without sun of the system as a whole number at days_editText
*/

hours     = (EditText) findViewById(R.id.hor_editText);
wh        = (EditText) findViewById(R.id.wh_editText);
daysNoSun = (EditText) findViewById(R.id.nosun_days_editText);

/**
* conversion to double of the decimal values entered 
* conversion to integer of the number of days without sun of the system
*/
numh      = Double.parseDouble(hours.getText().toString());
numwh     = Double.parseDouble(wh.getText().toString());
nosundays = Integer.parseInt(daysNoSun.getText().toString());

    /**
    * simple math 
    */
    double wattDay = numh * numwh;
    double wattSys = wattDay*nosundays;

您可以通过多种方式从EditText获取值,但是如果您想做的是确保操作首先确保这些。在编辑文本的XML布局中,确保
android:inputType
设置为
“number”
“numberSigned”
“numberDecimal”
,这样用户就不会干扰我们的应用程序

现在,在java类上,根据预期的输出,将数字的参数全局初始化为0,可以是整数(int)表示整数之间的和或减(inputType number或numberSigned),也可以是更一般的双精度表示所有操作

最后,您需要做的是解析信息,而不是将其作为文本字符串获取,因为它涉及两个不必要的转换

public void wattSystemView(View view) {

/**
* decimal values entered in hor_editText and wh_editText
* number of days without sun of the system as a whole number at days_editText
*/

hours     = (EditText) findViewById(R.id.hor_editText);
wh        = (EditText) findViewById(R.id.wh_editText);
daysNoSun = (EditText) findViewById(R.id.nosun_days_editText);

/**
* conversion to double of the decimal values entered 
* conversion to integer of the number of days without sun of the system
*/
numh      = Double.parseDouble(hours.getText().toString());
numwh     = Double.parseDouble(wh.getText().toString());
nosundays = Integer.parseInt(daysNoSun.getText().toString());

    /**
    * simple math 
    */
    double wattDay = numh * numwh;
    double wattSys = wattDay*nosundays;

作为下面答案的补充,在用户输入无效条目(即非数字)时进行解析时,不要忘记捕获
numberformatception
。作为下面答案的补充,在用户输入无效条目时进行解析时,不要忘了捕获
numberformatception
(即非数字)。谢谢大家的帮助!谢谢大家的帮助!