EditText到Int

gra*_*nra 2 java int android integer android-edittext

我是Java和Android开发的新手,所以我决定创建一个简单的应用程序,它接收两个字段并将它们放在一个简单的数学方程式中.但是我必须先将EditText转换为Int才能用它们进行任何数学运算,对吗?在XML文件中,我将EditTexts都设置为android:inputType ="number".我的尝试是这样的:

  final EditText height = (EditText) findViewById (R.id.height);
  String heightString = height.getText().toString();
  final int heightInt = Integer.parseInt(heightString);
Run Code Online (Sandbox Code Playgroud)

  final EditText height = (EditText) findViewById (R.id.height);
  final int heightInt = Integer.parseInt(height.getText().toString());
Run Code Online (Sandbox Code Playgroud)

我在两种情况下都尝试过Integer.valueOf但是当我运行它时它总是强制关闭我的应用程序并且如果我注释掉int heightInt它不强制关闭.

这是整个代码:

package com.body.calculator;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class bmi extends Activity {
     /** Called when the activity is first created. */



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView results = (TextView) findViewById(R.id.results);
    final EditText height = (EditText) findViewById (R.id.height);
    String heightString = height.getText().toString();
    final int heightInt = Integer.parseInt(heightString);
    final EditText weight = (EditText) findViewById (R.id.weight);
    final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    Button submit_bmi = (Button) findViewById(R.id.submit_bmi);
    submit_bmi.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (height.getText().toString().equals("") && weight.getText().toString().equals("")) {
                results.setText("Both fields are empty");
                vib.vibrate(100);
            }
            else if (height.getText().toString().equals("")) {
                results.setText("Height is empty");
                vib.vibrate(100);
            }
            else if (weight.getText().toString().equals("")) {
                results.setText("Weight is empty");
                vib.vibrate(100);
            }
            else {
            results.setText("");    
            }
        }


    });

}
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.谢谢,阿纳尔.

San*_*eer 5

你接近了

  final EditText height = (EditText) findViewById (R.id.height);
  String heightString = height.getText().toString();
  final int heightInt = Integer.parseInt(heightString);
Run Code Online (Sandbox Code Playgroud)

是正确的,但你直接在你的onCreate方法中调用它.这意味着即使在用户能够执行任何操作之前,应用程序启动时也会立即调用它们.因为这个值总是空的,只是因为用户甚至还没有做任何改变.

当您稍后使用这些值onClick(这是正确的)时,由于它们的早期初始化,它们仍然是空的.

试试这个部分:

String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
Run Code Online (Sandbox Code Playgroud)

直接在您的onClick方法开始之后但在您的if/else语句之前如此:

public void onClick(View v) {
    String heightString = height.getText().toString();
    final int heightInt = Integer.parseInt(heightString);

           // Your statements

  }
Run Code Online (Sandbox Code Playgroud)