英文:
Android Studio Java: How to I store the value of an EditText Input as double?
问题
package com.example.durchschnittrechnen;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
EditText et_zahl1, et_zahl2, et_zahl3, et_zahl4, et_zahl5;
TextView tv_ergebnis;
Button btn_berechnen, btn_reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
reset();
calculateAverage();
}
public void setupUI() {
et_zahl1 = findViewById(R.id.et_zahl1);
et_zahl2 = findViewById(R.id.et_zahl2);
et_zahl3 = findViewById(R.id.et_zahl3);
et_zahl4 = findViewById(R.id.et_zahl4);
et_zahl5 = findViewById(R.id.et_zahl5);
btn_berechnen = findViewById(R.id.btn_berechnen);
btn_reset = findViewById(R.id.btn_reset);
tv_ergebnis = findViewById(R.id.tv_ergebnis);
}
public void reset() {
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_zahl1.setText("");
et_zahl2.setText("");
et_zahl3.setText("");
et_zahl4.setText("");
et_zahl5.setText("");
tv_ergebnis.setText("");
}
});
}
public void calculateAverage() {
btn_berechnen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double input1 = Double.parseDouble(et_zahl1.getText().toString());
double input2 = Double.parseDouble(et_zahl2.getText().toString());
double input3 = Double.parseDouble(et_zahl3.getText().toString());
double input4 = Double.parseDouble(et_zahl4.getText().toString());
double input5 = Double.parseDouble(et_zahl5.getText().toString());
double average = (input1 + input2 + input3 + input4 + input5) / 5;
tv_ergebnis.setText(String.valueOf(average));
}
});
}
}
Please note that the provided translation contains only the translated code portion without any additional content.
英文:
so I am working on a small application which reads 5 different inputs (double type) from 5 different EditTexts.
So each EditText field represents 1 input containing a double value.
A buttom named "calculate" then calculates the average by using (input1 + input2 + input3 + input4 + input5 / 5) and stores it into a TextView called "average".
My question is how do I access the input to use it in my calculation method?
This is what my code looks like so far:
package com.example.durchschnittrechnen;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
EditText et_zahl1, et_zahl2, et_zahl3, et_zahl4, et_zahl5;
TextView tv_ergebnis;
Button btn_berechnen, btn_reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
reset();
calculateAverage();
}
public void setupUI() {
et_zahl1 = findViewById(R.id.et_zahl1);
et_zahl2 = findViewById(R.id.et_zahl2);
et_zahl3 = findViewById(R.id.et_zahl3);
et_zahl4 = findViewById(R.id.et_zahl4);
et_zahl5 = findViewById(R.id.et_zahl5);
btn_berechnen = findViewById(R.id.btn_berechnen);
btn_reset = findViewById(R.id.btn_reset);
tv_ergebnis = findViewById(R.id.tv_ergebnis);
}
public void reset() {
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_zahl1.setText("");
et_zahl2.setText("");
et_zahl3.setText("");
et_zahl4.setText("");
et_zahl5.setText("");
tv_ergebnis.setText("");
}
});
}
public void calculateAverage() {
btn_berechnen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
I have no idea how I proceed now.
Any help is welcome.
Thanks.
答案1
得分: 2
有一个getText()函数。它提供了文本框中的任何文本。然后您需要将其转换为Double。
String str = mEdit.getText().toString();
double dnum = Double.parseDouble(str);
英文:
There is a getText() function. It provides whatever text is in the box. You then would have to convert it to a Double.
String str = mEdit.getText().toString();
double dnum = Double.parseDouble(str);
答案2
得分: 0
编辑文本值返回字符串,您可以将字符串转换为双精度数。这里是一个示例:
double et_zahl1_dbl = new Double(et_zahl1.getText().toString());
我建议在用户输入除双精度数以外的内容时添加用户消息提示。
英文:
Edit Text value returns string, you can covert string to double. Here is an example:
double et_zahl1_dbl = new Double(et_zahl1.getText().toString());
I do recommend adding user messaging, if user enters anything else besides double.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论