Android - 计算edittext的值加1并显示它

时间:2016-01-10 15:04:48

标签: android android-intent bundle

我正在尝试将数据从一个活动传递到另一个活动。 当我从我创建的包中检索数据时,它返回null而不是它应该的数字。

这是我的代码,我在其中定义数字并将其传递给我的新活动:

public class SelectDrinks extends MainActivity {
    int AddOneBeer;
    int SubstractOneBeer;
    String AmountOfHoursString;
    String AmountOfBeersString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectdrinks);

        final ImageButton btnBack = (ImageButton) findViewById(R.id.imgBtnBack);
        final Button btnConfirm = (Button) findViewById(R.id.btnSelectedDrinksConfirm);
        final TextView AmountOfHours = (TextView) findViewById(R.id.tvDurationCalc);
        final TextView AmountOfBeers = (TextView) findViewById(R.id.tvAmountOfBeers);
        ImageView plusBeers = (ImageView) findViewById(R.id.btnDrink1);
        ImageButton minBeers = (ImageButton) findViewById(R.id.minBeers);


        btnBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent goBackToMain = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(goBackToMain);
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent goToStep3 = new Intent(getApplicationContext(), Calculate.class);
                goToStep3.putExtra("AmountOfHours", AmountOfHoursString);
                goToStep3.putExtra("AmountOfBeers", AmountOfBeersString);
                startActivity(goToStep3);
            }
        });

        plusBeers.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddOneBeer = Integer.parseInt(AmountOfBeers.getText().toString());
                AddOneBeer++;
                AmountOfBeers.setText(Integer.toString(AddOneBeer));
            }
        });

        minBeers.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                SubstractOneBeer = Integer.parseInt(AmountOfBeers.getText().toString());
                SubstractOneBeer--;
                AmountOfBeers.setText(Integer.toString(SubstractOneBeer));
            }
        });
    }
}

然后我尝试在我的新活动中使用AmountOfBeersString和AmountOfHoursString。启动时,两个数字都表示“空”。

public class Calculate extends MainActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate);

        final ImageButton btnBack = (ImageButton) findViewById(R.id.imgBtnBack);
        final Button btnConfirm = (Button) findViewById(R.id.btnCalculateConfirm);
        TextView changeRequest = (TextView) findViewById(R.id.tvEditRequest);
        TextView tvSummaryBeers = (TextView) findViewById(R.id.tvSummaryBeers);

        Bundle bundle = getIntent().getExtras();
        String AmountOfHoursString = bundle.getString("AmountOfHoursString");
        String AmountOfBeersString = bundle.getString("AmountOfBeersString");

        tvSummaryBeers.setText("You drank " + AmountOfBeersString + " beer(s) in " + AmountOfHoursString + " hour(s).");

        btnBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent goBackToMain = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(goBackToMain);
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent calculate = new Intent(getApplicationContext(), Result.class);
                startActivity(calculate);
            }
        });

        changeRequest.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent back = new Intent(getApplicationContext(), SelectDrinks.class);
                startActivity(back);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

Calculate活动中,更改

String AmountOfHoursString = bundle.getString("AmountOfHoursString");
String AmountOfBeersString = bundle.getString("AmountOfBeersString");

String AmountOfHoursString = bundle.getString("AmountOfHours");
String AmountOfBeersString = bundle.getString("AmountOfBeers");

您使用的密钥与您用于首先发送数据的密钥不同。

答案 1 :(得分:1)

您需要从TextView获取文本

 goToStep3.putExtra("AmountOfHours", AmountOfHours.getText().toString() );
 goToStep3.putExtra("AmountOfBeers", AmountOfBeers.getText().toString() );

AmountOfHoursStringAmountOfHoursString是声明的字符串,但我看不到它们在任何地方初始化。

还要确保使用相同的密钥从数据包中获取数据

 String AmountOfHoursString = bundle.getString("AmountOfHours");
 String AmountOfBeersString = bundle.getString("AmountOfBeers");