如何采取两种的EditText字段的值,进行简单的数学 [英] How to take the value of two EditText fields and perform simple mathematics
问题描述
我是新Android开发,并正在致力于用户在其中输入一个数字分成两个领域的EditText和$ P $一个简单的练习项目psses标记为计算的按钮和两个数字的总和显示。这里是code我到目前为止,但我不知道如何将两个字符串值,并将其输出添加到名为一个TextView场答案:
公共无效calNumbers(查看视图){
文字的EditText =(EditText上)findViewById(R.id.edit_number1);
字符串值= text.getText()的toString()。 的EditText文本2 =(EditText上)findViewById(R.id.edit_number2);
字符串值2 = text2.getText()的toString()。 TextView的回答=(的TextView)findViewById(R.id.answer);
}
您必须将它们转换为数字(整型,浮点,长等),以preform你的算术。然后将结果转换回字符串在TextView中显示
INT VAL1 =的Integer.parseInt(值);
INT将val2 =的Integer.parseInt(值2);answer.setText(将String.valueOf(VAL1 + val2中));
I am new to Android development and am working on a simple practice project where the user enters a number into two EditText fields and presses a Button labeled "Calculate" and the sum of the two numbers is displayed. Here is the code I have so far, but I don't know how to add the two string values and output it to a TextView field named "answer":
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);
}
You'll have to convert them to numbers (int, float, long, etc) to preform your arithmetic. Then convert the result back to a String to display in a TextView
int val1 = Integer.parseInt(value);
int val2 = Integer.parseInt(value2);
answer.setText(String.valueOf(val1 + val2));
这篇关于如何采取两种的EditText字段的值,进行简单的数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!