gpt4 book ai didi

java - 如何检查两个 EditText 是否为空?

转载 作者:太空狗 更新时间:2023-10-29 15:53:26 25 4
gpt4 key购买 nike

如何检查两个 EditText 是否为空?我尝试了很多东西,但没有任何效果。当其中一个 EditText 为空时,我只想 toast 说“数据不完整”。我可以自己做 toast 部分。请帮助编辑文本部分。

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() 来获取文本。

if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString())){
//enterheight or entermass is empty
}

不要忘记检查 TextView API :-)

关于java - 如何检查两个 EditText 是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com