我正在构建一个应用程序,它根据A,B和C的用户输入解决二次公式。我遇到了问题。 A,B和C都是整数,但它们需要取EditText组件的值,即editText1(和2,3),因此公式有A,B,C运行。
我如何获得价值?这是我缺少此部分的代码:
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
}
}
});
}
}
答案 0 :(得分:4)
如果您阅读了API文档,则会看到您可以使用EditText#getText()
访问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.
}
答案 1 :(得分:0)
您可以使用以下内容将string
转换为int
或其他类型:
// convert text to an integer
int number = Integer.parseInt(editText1.getText().toString());
// convert text to a double
double number = Double.parseDouble(editText1.getText().toString());