How do I get the integer value of the Android EditText component?… here is a solution to the problem.
How do I get the integer value of the Android EditText component?
I’m building an application that solves quadratic formulas based on user input from A, B, and C. I have a question. A, B, and C are all integers, but they need to take the value of an EditText component editText1 (and 2 and 3), so the formula has A, B, C to run.
How do I get the value? Here is my code, which is missing this part :
double root1=0;
double root2=0;
double discriminant;
int A;
int B;
int C;
protected void onCreate(Bundle savedInstanceState) {
TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Called when the activity is first created. */
reset button will be a menu option
final TextView textView6 = (TextView) findViewById(R.id.textView6);
EditText inputA = (EditText)findViewById(R.id.editText1) ;
EditText inputB = (EditText)findViewById(R.id.editText2);
EditText inputC = (EditText)findViewById(R.id.editText3);
Button calcbutton = (Button)findViewById(R.id.calcbutton);
calcbutton.setOnClickListener(new View.OnClickListener() { // when calculate is clicked
public void onClick(View v) {
TODO Auto-generated method stub
discriminant = Math.sqrt((B*B)-(4*A*C));
if(discriminant>0){
root1 = ((-B + discriminant)/2*A);
root2 = ((-B - discriminant)/2*A);
set textview6 to answer above
textView6.setTag(root1 );
textView6.setTag(root2);
}
if(discriminant==0){
root1=(int) ((-B + discriminant)/2*A);
textView6.setTag(root1);
}
if(discriminant<0){
textView6.setText("This equation has imaginary roots");
equation has imaginary roots
}
}
});
}
}
Solution
If you read the API documentation, you’ll see that you can use the > EditText#getText() accesses the value in EditText
public void onClick(View v) {
A = Double.parseDouble(inputA.getText().toString());
B = Integer.parseInt(inputB.getText().toString());
C = Integer.parseInt(inputC.getText().toString());
discriminant = Math.sqrt((B*B)-(4*A*C));
etc.
}