2013-03-12 34 views
0

我正在編寫一個android應用程序來計算某人的身高和體重之間的關係。一切正常,我設法做算術。如何將textview的值放在if語句中

但是,當我想將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"); 
      } 
     } 
    }); 

} 
} 

回答

3

result.getText()的toString()長()> = 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"); 
} 
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"); 
       } 
0

使用此來從TextView

float f = Float.parseFloat(result.getText().toString()); 
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"); 
     } 
    } 
}); 

}}

+1

我不相信你可以使用類似18.5和24.9的值的int ... – TronicZomB 2013-03-12 15:34:22

+0

沒有注意到:-) – khurram 2013-03-13 14:00:28

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"); 
      } 
0

您應該直接在if語句中使用計算結果。不要把它放在一個textview中,你必須檢索它來進行比較。