0

我正在编写一个 android 应用程序来计算某人的身高和体重之间的关系。一切正常,我设法做算术。

但是,当我想将 TextView 的结果放在 if 语句中时,无论结果大于或小于或等于,我都会收到第一个 if 语句的输出。很快,我希望我的程序根据计算给出三个不同的结果。这是我的代码:

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

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // My Programming Begins Here
    final EditText editTextHeight = (EditText) findViewById(R.id.editTextHeight);
    final EditText editTextWeight = (EditText) findViewById(R.id.editTextWeight);
    Button calculateButton = (Button) findViewById(R.id.calculateButton);
    final TextView result = (TextView) findViewById(R.id.result);
    final TextView suggestionResult = (TextView) findViewById(R.id.suggestionResult);
    // Coding for the Button
    calculateButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int heightValue = Integer.parseInt(editTextHeight.getText()
                    .toString());

            int weightValue = Integer.parseInt(editTextWeight.getText()
                    .toString());

            // Finding out the square of weightValue and then dividing the
            // heightValue by the sqaure of weightValue
            result.setText(String.valueOf(heightValue
                    / (Math.sqrt(weightValue))));
            if (result.getText().toString().length() >= 1
                    && result.getText().toString().length() < 18.5) {
                suggestionResult.setText("You have to gain weight");
            }
            if (result.getText().toString().length() >= 18.5
                    && result.getText().toString().length() < 24.9) {
                suggestionResult.setText("You are normal");
            } else {
                suggestionResult.setText("You have to lose      weight");
            }
        }
    });

}
}
4

6 回答 6

4

尝试这个

double d=Double.parseDouble(result.getText().toString());

 if ( d<=18.5) {
                suggestionResult.setText("You have to gain weight");
            }
            if (d >= 18.5 && d < 24.9) {
                suggestionResult.setText("You are normal");
            } else {
                suggestionResult.setText("You have to lose  weight");
            }
于 2013-03-12T15:32:30.147 回答
3

结果.getText().toString().length() >= 18.5

你在这里做错了。它将给出editext中字符的长度而不是值,并且这个条件永远不会成立。

float rs = Float.parseFloat(result.getText().toString());

然后进行比较。

if (rs >= 1 && rs < 18.5f) {
    suggestionResult.setText("You have to gain weight");
}
if (rs >= 18.5f rs < 24.9f) {
    suggestionResult.setText("You are normal");
} else {
    suggestionResult.setText("You have to lose      weight");
}
于 2013-03-12T15:26:43.553 回答
0
 if (Double.ValueOf(result.getText()) >= 1
                && Double.ValueOf(result.getText()) < 18.5) {
                    suggestionResult.setText("You have to gain weight");
                }
                if (Double.ValueOf(result.getText()) >= 18.5
                        && Double.ValueOf(result.getText()) < 24.9) {
                    suggestionResult.setText("You are normal");
                } else {
                    suggestionResult.setText("You have to lose      weight");
                }
于 2013-03-12T15:30:08.623 回答
0

使用它从TextView

float f = Float.parseFloat(result.getText().toString());
于 2013-03-12T15:31:10.453 回答
0

像这样做 :-)

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

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

// My Programming Begins Here
final EditText editTextHeight = (EditText) findViewById(R.id.editTextHeight);
final EditText editTextWeight = (EditText) findViewById(R.id.editTextWeight);
Button calculateButton = (Button) findViewById(R.id.calculateButton);
final TextView result = (TextView) findViewById(R.id.result);
final TextView suggestionResult = (TextView) findViewById(R.id.suggestionResult);
// Coding for the Button
calculateButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        int heightValue = Integer.parseInt(editTextHeight.getText()
                .toString());

        int weightValue = Integer.parseInt(editTextWeight.getText()
                .toString());

        // Finding out the square of weightValue and then dividing the
        // heightValue by the sqaure of weightValue
        result.setText(String.valueOf(heightValue
                / (Math.sqrt(weightValue))));
        if (Integer.parseInt(result.getText().toString()) >= 1
                && result.getText().toString().length() < 18.5) {
            suggestionResult.setText("You have to gain weight");
        }
        if (Integer.parseInt(result.getText().toString()) >= 18.5
                && result.getText().toString().length() < 24.9) {
            suggestionResult.setText("You are normal");
        } else {
            suggestionResult.setText("You have to lose      weight");
        }
    }
});

} }

于 2013-03-12T15:31:34.417 回答
0

您应该直接在 if 语句中使用计算结果。不要把它放在你必须检索它进行比较的文本视图中。

于 2013-03-12T19:56:41.320 回答