0

我有一个问题,我想比较按钮中的数据

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?所以我可以进行比较

4

3 回答 3

0

Or you can use getTag(); setTag(); to store the variables on the buttons...

Use setTag to the data you want

     myButton.setTag("fun Stuff"); 

Then on the listener just get the tag (you will need to do a type cast):

         OnClickListener myListner = new OnClickListener() {
             public void onClick(View view) {
                 doSomethingAwesome(view, (String) view.getTag());
             }
         };
于 2012-05-25T00:03:49.930 回答
0

It's a scoping problem, you are declaring int TotalKalori locally in the onClick event.

Declare it like you have done with totalKalori1 and it should work

于 2012-05-24T22:05:47.290 回答
0

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

于 2012-05-26T15:54:39.743 回答