java-如何获取Android EditText组件的值作为整数?

我正在构建一个基于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
                }


            }
        });
     }
}

解决方法:

如果您阅读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.
}
上一篇:android-TextView文本未更新


下一篇:android-textview中的多个clickspan