如何检查两个编辑文本是否为空?我尝试了很多东西,但没有任何效果。我只想在任何一个 EditText 为空时干杯说"数据不完整"。我可以自己做吐司部分。请帮助编辑文本部分。
package com.maverick.mybmi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText entermass = (EditText) findViewById(R.id.entermass);
final EditText enterheight = (EditText) findViewById(R.id.enterheight);
Button BMI = (Button) findViewById(R.id.BMI);
BMI.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
double d = 0;
double mass = Double.valueOf(entermass.getText().toString());
double height = Double.valueOf(enterheight.getText().toString());
double heightF;
heightF=height*0.3048;
double bmi = mass/(heightF*heightF);
d=bmi;
int display = (int) bmi;
Toast toast=Toast.makeText(getApplicationContext(), "Your BMI is " +display, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
if(d<15.0)
{
Toast.makeText(MainActivity.this,"You Are Very Severely Underweight", Toast.LENGTH_LONG).show();
}
else if(d>=15.0 && d<=16.0){
Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
}
else if(d>=15.0 && d<=16.0){
Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
}
else if(d>=16.0 && d<=18.5){
Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
}
else if(d>=18.5 && d<=25.0){
Toast.makeText(MainActivity.this,"You Are Normal (healthy weight)", Toast.LENGTH_LONG).show();
}
else if(d>=25.0 && d<=30.0){
Toast.makeText(MainActivity.this,"You Are Overweight", Toast.LENGTH_LONG).show();
}
else if(d>=30.0 && d<=35.0){
Toast.makeText(MainActivity.this,"You Are Obese Class I (Moderately obese)", Toast.LENGTH_LONG).show();
}
else if(d>=35.0 && d<=40.0){
Toast.makeText(MainActivity.this,"You Are Obese Class II (Severely obese)", Toast.LENGTH_LONG).show();
}
else if(d>40.0){
Toast.makeText(MainActivity.this,"You Are Obese Class III (Very severely obese)", Toast.LENGTH_LONG).show();
}
}
});
};};
您可以使用 getText() 方法,然后对其调用 toString() 来获取 Text。
if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString())){
//enterheight or entermass is empty
}
不要忘记检查TextView API :-)
您可以使用TextUtils.isEmpty
. 它检查空值和空字符串:
if (TextUtils.isEmpty(entermass.getText().toString())) {
}