Android - How to get text from edittext and calcul

2019-08-24 02:39发布

问题:

I have an app, and I want to calculate the edit text. I have this code:

 package com.minimale.cijfer;

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.InputType;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;

 import com.minimale.cijfer.R;

 public class main extends Activity {
     /** Called when the activity is first created. */
      EditText gehaald_een;
      EditText gehaald_twee;
      EditText gehaald_drie;
      EditText gehaald_een_weging;
      EditText gehaald_twee_weging;
          EditText gehaald_drie_weging;
      EditText nieuw;
      EditText nieuw_weging;
      EditText finalResult;


     OnClickListener radioGroup;


     double een;
     double twee;
     double drie;
     double vier;
     double een_c;
     double twee_c;
     double drie_c;
     double nieuw_c;
     double weging_t;
     double answer;


     Button addButton;
     Button divideButton;
     Button multiplyButton;
     Button subtractButton;
     Button final_calculation;


    @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         gehaald_een = (EditText)findViewById(R.id.editText1);
         gehaald_twee = (EditText)findViewById(R.id.editText2);
         gehaald_drie = (EditText)findViewById(R.id.editText3);
         gehaald_een_weging = (EditText)findViewById(R.id.editText4);
         gehaald_twee_weging = (EditText)findViewById(R.id.editText5);
         gehaald_drie_weging = (EditText)findViewById(R.id.editText6);
         finalResult = (EditText)findViewById(R.id.answer);

         nieuw = (EditText)findViewById(R.id.editText7);
         nieuw_weging = (EditText)findViewById(R.id.editText8);


         divideButton =(Button)findViewById(R.id.button1);
         divideButton.setOnClickListener(new DivideButtonHandler());




     }


    private class DivideButtonHandler implements OnClickListener{

       @Override
       public void onClick(View v) {
          // TODO Auto-generated method stub
           een = Double.parseDouble(gehaald_een_weging.getText().toString());
           twee = Double.parseDouble(gehaald_twee_weging.getText().toString());
           drie = Double.parseDouble(gehaald_drie_weging.getText().toString());
           vier = Double.parseDouble(nieuw_weging.getText().toString());
           een_c = Double.parseDouble(gehaald_een.getText().toString());
           twee_c = Double.parseDouble(gehaald_twee.getText().toString());
           drie_c = Double.parseDouble(gehaald_drie.getText().toString());
           nieuw_c = Double.parseDouble(nieuw.getText().toString());
           weging_t = Double.parseDouble(nieuw_weging.getText().toString());


           answer = ((een + twee + drie + vier) * nieuw_c - ((een_c * een) + (twee *                twee_c)) / weging_t);
            finalResult.setText(answer + "");

       }

    }



 }

But when I run my app, and fill the edittext boxes in and press on the button, my app force closes. Does anybody know what I did wrong?

回答1:

use

Double.valueOf(String s);

instead of

Double.parseDouble();

and also replace

finalResult.setText(answer + "");

with

finalResult.setText(Double.toString(answer) + "");