如何比较第一个中的数据(int)和Android中第二个按钮中的数据(int)?

时间:2012-05-24 21:56:15

标签: android button

我有一个问题,我想在比较按钮

中的数据
package lynn.calculate.KaunterKalori;

import lynn.calculate.KaunterKalori.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;

public class CalculateAll extends Activity implements OnClickListener {


protected int totalKalori1;
protected int totalKalori2;
protected int totalKalori3;

EditText EstimateCalorie;
TextView TotalKaloriSehari;
TextView totalsarapan;
TextView totallunch;
TextView totaldinner;
TextView calorieneeds;
TextView resultDiff;

public CalculateAll() {

}

@Override
public void onCreate(Bundle savedInstancesState) {
    super.onCreate(savedInstancesState);
    setContentView(R.layout.calculate_all);

    View kiraButton = findViewById(R.id.buttonKiraAll);
    kiraButton.setOnClickListener(this);

    View bezaButton = findViewById(R.id.caloriediff);
    bezaButton.setOnClickListener(this);

    TotalKaloriSehari = (TextView) findViewById(R.id.JumlahKalori);
    totalsarapan = (TextView) findViewById(R.id.sarapantext);
    totallunch = (TextView) findViewById(R.id.lunchtext);
    totaldinner = (TextView) findViewById(R.id.dinnertext);
    EstimateCalorie = (EditText) findViewById(R.id.BMR);
    // calorieneeds = (TextView) findViewById(R.id.BMR);
    resultDiff = (TextView) findViewById(R.id.result);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        totalKalori1 = extras.getInt("totalBreakfast");
        totalKalori2 = extras.getInt("totalLunch");
        totalKalori3 = extras.getInt("totalDinner");
    }

    totalsarapan.setText(totalKalori1 + "");
    totallunch.setText(totalKalori2 + "");
    totaldinner.setText(totalKalori3 + "");



}

public void onClick(View v) {
    if (v.getId() == R.id.buttonKiraAll) {


     int TotalKalori = calculateTotalKalori(totalKalori1, totalKalori2,
                totalKalori3);
        TotalKaloriSehari.setText(TotalKalori + "");

    }

    else if (v.getId()== R.id.caloriediff) {

        int nilaikalori = Integer.parseInt(EstimateCalorie.getText()
                .toString());


        int bmr = calculatebmr(nilaikalori);

        String deskripsiKalori = describekalori(bmr);
        resultDiff.setText("kalori makanan" + TotalKalori + " calori  diperlukan " + bmr + "=" + deskripsiKalori); 
// --> this TotalKalori also can't be used


        Intent n = new Intent(this, MainActivity.class);
        startActivity(n);

    }

}




public int calculateTotalKalori(int totalKalori1, int totalKalori2,
        int totalKalori3) {
    return (int) (totalKalori1 + totalKalori2 + totalKalori3);

}

public int calculatebmr(int nilaikalori) {
    return (int) (nilaikalori);
}

private String describekalori(int bmr) { 

    if (bmr < TotalKalori ) { //<-- this TotalKalori can't be read/used
        return "bykkan makan";
    } else if (bmr == TotalKalori) {

        return "kekalkan jumlah kalori ini";

    } else if (bmr > TotalKalori) {

        return "kurangkan pengambilan kalori";

    } else
        return "oiii";

} 
}

第一个按钮,buttonKiraAll读取int totalKalori,然后我想使用totalKalori数据与第二个数据按钮calorieDiff进行比较,我想如何让totalKalori可以通过第二个按钮读取?所以我可以进行比较

3 个答案:

答案 0 :(得分:0)

这是一个范围问题,您在onClick事件中本地声明 int TotalKalori

声明它就像你使用 totalKalori1 一样,它应该可以工作

答案 1 :(得分:0)

或者你可以使用getTag(); setTag();将变量存储在按钮上......

将setTag用于您想要的数据

     myButton.setTag("fun Stuff"); 

然后在侦听器上获取标签(您需要进行类型转换):

         OnClickListener myListner = new OnClickListener() {
             public void onClick(View view) {
                 doSomethingAwesome(view, (String) view.getTag());
             }
         };

答案 2 :(得分:0)

仅全局声明 int TotalKalori (在您声明为protected int totalKalori1; protected int totalKalori2;...的部分中),并将其用于以下两个按钮:in button1 TotalKalori = calculateTotalKalori(totalKalori1,totalKalori2,totalKalori3);这个值是全局的,所以button2已经可以通过简单地使用你需要的TotalKalori以及比较部分来获取它。