当我单击第二个微调器项目时,该特定微调器项目的代码应执行但未执行。如果正在执行部分,则仅执行一个
我尝试了以下代码
package com.example.bmicalculator;
import android.graphics.Color; import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.shashank.sony.fancytoastlib.FancyToast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar;
import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity { Spinner gender_spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);
// Get the references to the widgets
final EditText weight_et = (EditText) findViewById(R.id.weight_et);
final EditText height_et = (EditText) findViewById(R.id.height_et);
final EditText height_et1 = (EditText) findViewById(R.id.ft_in_et);
final EditText weight_et1 =(EditText)findViewById(R.id.st_lb_et);
final TextView result_tv = (TextView) findViewById(R.id.result_tv);
final TextView ideal_weight = (TextView)findViewById(R.id.ideal_weight);
final Spinner height_spinner = findViewById(R.id.height_spinner);
ArrayAdapter<CharSequence> adapter_height = ArrayAdapter.createFromResource(this,R.array.Height,android.R.layout.simple_spinner_item);
adapter_height.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
height_spinner.setAdapter(adapter_height);
height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
height_et.setHint("Height(cm)");
height_et1.setVisibility(View.GONE);break;
case 1:
height_et1.setHint("in");
height_et1.setVisibility(View.VISIBLE);
height_et.setHint("ft");break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
FancyToast.makeText(getApplicationContext(),"Error",FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
}
});
final Spinner weight_spinner= findViewById(R.id.weight_spinner);
ArrayAdapter<CharSequence> adapter_weight = ArrayAdapter.createFromResource(this,R.array.Weight,android.R.layout.simple_spinner_item);
adapter_weight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weight_spinner.setAdapter(adapter_weight);
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
weight_et.setHint("Weight(kg)");
weight_et1.setVisibility(View.GONE);break;
case 1:
weight_et.setHint("Weight(lb)");
weight_et1.setVisibility(View.GONE);break;
case 2:
weight_et.setHint("st");
weight_et1.setVisibility(View.VISIBLE);
weight_et1.setHint("lb");break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
FancyToast.makeText(getApplicationContext(),"Error",FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
}
});
gender_spinner = findViewById(R.id.gender_spinner);
ArrayAdapter<CharSequence> adapter_gender = ArrayAdapter.createFromResource(this,R.array.Gender,android.R.layout.simple_spinner_item);
adapter_gender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender_spinner.setAdapter(adapter_gender);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
weight_et.setText("");
height_et.setText("");
height_et1.setText("");
height_et1.setVisibility(View.GONE);
weight_et1.setText("");
weight_et1.setVisibility(View.GONE);
result_tv.setText("");
ideal_weight.setText("");
height_spinner.setSelection(0);
weight_spinner.setSelection(0);
}
});
if (height_spinner.getSelectedItemPosition()==0 && weight_spinner.getSelectedItemPosition()==0){
findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {
// Logic for validation, input can't be empty
@Override
public void onClick(View v) {
String Height = height_et.getText().toString().trim();
String Weight = weight_et.getText().toString().trim();
if(Height.isEmpty()){
height_et.setError("Please enter your Height");
height_et.requestFocus();
return;
}
if(Weight.isEmpty()){
weight_et.setError("Please enter your Weight");
weight_et.requestFocus();
return;
}
//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float height = Float.parseFloat(Height)/100;
//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight
//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);
if (bmiValue<16) {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >=16 && bmiValue<=17){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>17 && bmiValue <18.5) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>=18.5 && bmiValue <= 25) {
result_tv.setTextColor(Color.parseColor("#008000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
}
else if ( bmiValue > 25 && bmiValue < 30) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 30 && bmiValue <35){
result_tv.setTextColor(Color.parseColor("#FF4500"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 35 && bmiValue <40){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
}
});
}
if (height_spinner.getSelectedItemPosition()==1 && weight_spinner.getSelectedItemPosition()==0){
findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {
// Logic for validation, input can't be empty
@Override
public void onClick(View v) {
String Height = height_et.getText().toString().trim();
String Weight = weight_et.getText().toString().trim();
String Height1 = height_et1.getText().toString().trim();
if(Height.isEmpty()){
height_et.setError("Please enter feet");
height_et.requestFocus();
return;
}
if(Weight.isEmpty()){
weight_et.setError("Please enter your Weight(in kg)");
weight_et.requestFocus();
return;
}
if (Height1.isEmpty()){
height_et1.setError("Please Enter inches");
height_et1.requestFocus();
return;
}
//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float feet = Float.parseFloat(Height); float inches = Float.parseFloat(Height1); float height = convertftandin(feet,inches)/100;
//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight
//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);
if (bmiValue<16) {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >=16 && bmiValue<=17){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>17 && bmiValue <18.5) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>=18.5 && bmiValue <= 25) {
result_tv.setTextColor(Color.parseColor("#008000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
}
else if ( bmiValue > 25 && bmiValue < 30) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 30 && bmiValue <35){
result_tv.setTextColor(Color.parseColor("#FF4500"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 35 && bmiValue <40){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
}
});
}
}
//Calculate BMI
private float calculateBMI (float weight, float height) {
return (float) (weight / (height * height));
}
//conversion of ft and in to cm
private float convertftandin(float ft,float in){
return ((ft*30.48f)+(in*2.54f));
}
//Calculate Ideal Weight
// Interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Very Severely Underweight";
}
else if (bmiValue >=16 && bmiValue<=17){
return "Severely Underweight";
}
else if (bmiValue>17 && bmiValue <18.5) {
return "Underweight";
}
else if (bmiValue>=18.5 && bmiValue <= 25) {
return "Normal";
}
else if ( bmiValue > 25 && bmiValue < 30) {
return "Overweight";
}
else if (bmiValue >= 30 && bmiValue <35){
return "Obese Class I";
}
else if (bmiValue >= 35 && bmiValue <40){
return "Obese Class II";
}
else {
return "Obese Class III";
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_remove_ads) {
return true;
}
return super.onOptionsItemSelected(item);
}
} 当我单击第二个微调器项目时,应执行该特定微调器项目的代码,但未执行。只有一部分正在执行时, 以下代码不起作用
if (height_spinner.getSelectedItemPosition()==1 && weight_spinner.getSelectedItemPosition()==0){
findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {
// Logic for validation, input can't be empty
@Override
public void onClick(View v) {
String Height = height_et.getText().toString().trim();
String Weight = weight_et.getText().toString().trim();
String Height1 = height_et1.getText().toString().trim();
if(Height.isEmpty()){
height_et.setError("Please enter feet");
height_et.requestFocus();
return;
}
if(Weight.isEmpty()){
weight_et.setError("Please enter your Weight(in kg)");
weight_et.requestFocus();
return;
}
if (Height1.isEmpty()){
height_et1.setError("Please Enter inches");
height_et1.requestFocus();
return;
}
//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float feet = Float.parseFloat(Height); float inches = Float.parseFloat(Height1); float height = convertftandin(feet,inches)/100;
//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight
//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);
if (bmiValue<16) {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >=16 && bmiValue<=17){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>17 && bmiValue <18.5) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue>=18.5 && bmiValue <= 25) {
result_tv.setTextColor(Color.parseColor("#008000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
}
else if ( bmiValue > 25 && bmiValue < 30) {
result_tv.setTextColor(Color.parseColor("#9ACD32"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 30 && bmiValue <35){
result_tv.setTextColor(Color.parseColor("#FF4500"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else if (bmiValue >= 35 && bmiValue <40){
result_tv.setTextColor(Color.parseColor("#FF6347"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
else {
result_tv.setTextColor(Color.parseColor("#FF0000"));
result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
ideal_weight.setTextColor(Color.parseColor("#008000"));
}
}
});```
要R.id.ibl根据选择的Spinner 设置onClickListener 。
但是,执行此操作的部分不在Spinner的侦听器内部。这就是为什么它们不会在每次单击特定微调器时执行。
您的代码仅执行下面的代码块,if (height_spinner.getSelectedItemPosition()==0 && weight_spinner.getSelectedItemPosition()==0)因为它们是微调器的初始值。
您可能需要将代码块设置为Spinners' onClickListener的R.id.ibl内部onItemSelectedListener。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。