【问题标题】:How can I use a single button click to achieve multiple actions in android eclipse?如何在 android eclipse 中使用单个按钮单击来实现多个操作?
【发布时间】:2015-06-02 12:50:45
【问题描述】:

我是使用 Eclipse 开发 JavaAndroid 的新手,所以请原谅我的“愚蠢”问题(以及我的绰号)。

我正在创建一个应用程序,它接收 user inputs- 体重和身高,计算用户的 BMI(身体质量指数)double bmi = weight/(height * height); 并在 textView 中显示 output。我已经做到了这一点,但我一直在思考如何:

首先,使用单个buttonclick在一个textView1上显示BMI值;

其次,display 在不同的textView2 上使用text,具体取决于用户的BMI 值落入的范围,使用下面的if 语句:

if(bmi >= 0 && bmi < 22.01)
        {
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is too low, you are underweight.\nEat regularly and put on some weight.");
        }else if(bmi >= 22.01 && bmi < 25.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is normal, maintain your healthy lifestyle.");
        }else if(bmi >= 25.01 && bmi < 30.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is high, you are overweight!\nGet on a diet and exercise regularly.");
        }else if(bmi >= 30.01 && bmi < 40.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is very high, you are OBESE!\nGet on a diet and exercise as often as you can.");
        }else{
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.println("That is way too high, you are SEVERELY OBESE!\nGet on a diet asap and exercise everyday!");
        }

我已经搜索过这个网站和其他网站,但在这方面没有足够的帮助。 非常感谢,期待您的帮助!

【问题讨论】:

  • 感谢@SaketMittal,现在就去看看。

标签: java android eclipse


【解决方案1】:

如何检测点击?在 XML 文件中使用 onClickListener 还是使用 onClick 标记? 无论哪种方式,您都会在您的活动中获得一个方法,该方法将在您的按钮被单击时执行。只需将应该执行的所有代码都放入此方法中。

【讨论】:

  • 谢谢@Jonas,我用 onClickListener 检测它,而不是 XML 文件中的 onClick 标记。我会试一试,然后告诉你。
  • 效果很好。按照@Didi78 的建议修改了我的代码,并且正如我预期的那样流畅。感谢您的帮助。
【解决方案2】:

您应该使用 2 个 editText 用于输入,并使用 2 个 TextView 来显示 BMI 并显示“Secendly”:

在您的主要活动 xml 中:

 <EditText 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/Weight"
    android:hint="@string/Weight"
    android:inputType="number"/>

 <EditText 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/Height"
    android:hint="@string/Height"
    android:inputType="number"/>

    <Button
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/Height"
    android:id="@+id/button"
    android:text="BMI" />

    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/BMI"
    android:layout_marginBottom="10dp" />

    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/BMItext"
    android:layout_marginBottom="10dp" />

在你的代码中:

public class MainActivity extends Activity {

private EditText Weight, Height;
private TextView BMI, BMItext;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BMItext = (TextView) findViewById(R.id.BMItext);        
    BMI = (TextView) findViewById(R.id.BMI);        
    Weight = (EditText) findViewById(R.id.Weight);
    Height = (EditText) findViewById(R.id.Height);  
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           Double Weightnum= Weight.getText().toString();
           Double WeightHeight = Weight.getText().toString()*Height.getText().toString();
            Double calculator = Weightnum/WeightHeight;
            BMI.setText("Your BMI is:" + calculator);
if(calculator >= 0 && calculator < 22.01)
    {
BMItext.settext("That is too low, you are underweight.\nEat regularly and put on some weight.");
                }else if(calculator >= 22.01 && calculator < 25.01){
        BMItext.settext("That is normal, maintain your healthy lifestyle.");
    }else if(calculator >= 25.01 && calculator < 30.01){
        BMItext.settext("That is high, you are overweight!\nGet on a diet and exercise regularly.");
    }else if(bmi >= 30.01 && bmi < 40.01){
        BMItext.settext("That is very high, you are OBESE!\nGet on a diet and exercise as often as you can.");
    }else{
        BMItext.settext("That is way too high, you are SEVERELY OBESE!\nGet on a diet asap and exercise everyday!");
    }

        }
    });

    }
 }

我希望我没有错过任何东西,我刚刚在这里创建了这段代码,所以.. :)

【讨论】:

  • 我还没有实现这个,但我看到了逻辑,看起来它会起作用(当然没有 settext 拼写错误 ;-))。我会在申请后立即告诉您结果,非常感谢。
  • 我使用您的逻辑调整了我的代码,它奏效了。希望我能给你投票,但我还没有足够的声誉。谢谢一百万!
  • 你可以点击V :)
  • 哦,好的。对不起,还是要了解绳索。干杯! ??
猜你喜欢
  • 2020-10-07
  • 1970-01-01
  • 2022-12-18
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 2022-11-01
  • 1970-01-01
相关资源
最近更新 更多