Android-如何计算一个编辑文本并除以另一个值?

Android-如何计算一个编辑文本并除以另一个值?,android,android-edittext,Android,Android Edittext,我是一个完全的初学者,我想知道是否有人可以帮助我的代码。我正在尝试制作理想的日常饮水应用程序 将只有一个编辑文本供用户输入他们的重量,我希望它除以例如0.024。有按钮计算,然后在屏幕上显示答案 public class WaterCalculate extends Activity { //Declare textviews as fields, so they can be accessed throughout the activity. EditText weightu

我是一个完全的初学者,我想知道是否有人可以帮助我的代码。我正在尝试制作理想的日常饮水应用程序

将只有一个编辑文本供用户输入他们的重量,我希望它除以例如0.024。有按钮计算,然后在屏幕上显示答案

public class WaterCalculate extends Activity {

    //Declare textviews as fields, so they can be accessed throughout the activity.
    EditText weightuser;
    TextView tv4; 
    ImageButton calculate;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.water_calculate);

        //Bind the EditText views

        weightuser = (EditText)findViewById(R.id.weight);
        tv4 = (TextView)findViewById(R.id.res);

        calculate =  (ImageButton)findViewById(R.id.calc);
        calculate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               calculate();
        }
        });
    }

        private void calculate() {
             //get entered texts from the edittexts,and convert to integers.
            Double value1 = Double.parseDouble(weightuser.getText().toString());
            //do the calculation
            Double calculatedValue = (value1/0.024);
            //set the value to the textview, to display on screen.
            tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
        }

}

当我运行应用程序时,按下按钮计算其不工作,并显示应用程序已停止。感谢您的帮助。

请尝试下面的代码,我相信它会起作用,当您使用“单击侦听器”按钮时,您应该使用“查看”。Onclick

class WaterCalculate extends Activity {

    // Declare textviews as fields, so they can be accessed throughout the
    // activity.
    EditText weightuser;
    TextView tv4;
    ImageButton calculate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.water_calculate);

        // Bind the EditText views

        weightuser = (EditText) findViewById(R.id.weight);
        tv4 = (TextView) findViewById(R.id.res);

        calculate = (ImageButton) findViewById(R.id.calc);

        calculate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calculate();
            }
        });
    }

    private void calculate() {
        // get entered texts from the edittexts,and convert to integers.
        Double value1 = Double.parseDouble(weightuser.getText().toString());
        // do the calculation
        Double calculatedValue = (value1 / 0.024);
        // set the value to the textview, to display on screen.
        tv4.setText(String.valueOf("You need " + calculatedValue
                + "\nliters of water per day"));
    }

}

我以为问题出在这方面

 tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
像这样试一试

tv4.setText("You need "+Double.toString(calculatedValue)+"\nliters of water per day");

同时还要确保在必要的位置捕捉到异常,例如将editText值转换为double。

我已经尝试过,但当我单击按钮时,应用程序仍然停止。谢谢你的回答@MadhuSorry,但我没有点击它。但是你的回答很有用。再次感谢你@MadhuI已经将其更改为字符串,但仍然无法工作。也许代码很好,但我不知道错误在哪里。顺便说一句,谢谢@Vishwa@Ell首先确保父布局中有可用的视图ID。然后在try-and-catch中尝试整个代码。您将获得异常类型。基于异常将帮助您找到解决方案。