如何将总数从一个类传递到另一个类并通过toast消息显示该总数

时间:2014-06-25 13:36:54

标签: android

按钮add1,sub1,add2,sub2,add3,sub3,add4,sub4,order;     TextView qtyfrenchfries,amt_frenchfries,qtychilifries,amt_chilifries,             qtypotatofries,amt_potatofries,qtygrilled,amt_grilled,total;

int count1, count2, count3, count4;
double priceval1, priceval2, priceval3, priceval4;
CheckBox frenchfries, chilicheese, sweetpotato, grilledvegetables;

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

    frenchfries = (CheckBox) findViewById(R.id.checkBox1);
    chilicheese = (CheckBox) findViewById(R.id.checkBox2);
    sweetpotato = (CheckBox) findViewById(R.id.checkBox3);
    grilledvegetables = (CheckBox) findViewById(R.id.checkBox4);

    count1 = 1;
    priceval1 = 2;
    qtyfrenchfries = (TextView) findViewById(R.id.textView1);
    amt_frenchfries = (TextView) findViewById(R.id.textView2);
    add1 = (Button) findViewById(R.id.button1);
    add1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count1++;
            qtyfrenchfries.setText("" + count1);
            priceval1 = priceval1 + 2;
            amt_frenchfries.setText("$" + priceval1);

        }
    });
    sub1 = (Button) findViewById(R.id.button2);
    sub1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count1--;
            if (count1 < 1) {
                count1 = 1;
            }
            qtyfrenchfries.setText("" + count1);
            priceval1 = priceval1 - 2;
            if (priceval1 <= 2) {
                priceval1 = 2;
            }
            amt_frenchfries.setText("$" + priceval1);

        }
    });

    count2 = 1;
    priceval2 = 3;
    qtychilifries = (TextView) findViewById(R.id.textView3);
    amt_chilifries = (TextView) findViewById(R.id.textView4);
    add2 = (Button) findViewById(R.id.button3);
    add2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count2++;
            qtychilifries.setText("" + count2);
            priceval2 = priceval2 + 3;
            amt_chilifries.setText("$" + priceval2);

        }
    });
    sub2 = (Button) findViewById(R.id.button4);
    sub2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count2--;
            if (count2 < 1) {
                count2 = 1;
            }
            qtychilifries.setText("" + count2);
            priceval2 = priceval2 - 3;
            if (priceval2 <= 3) {
                priceval2 = 3;
            }
            amt_chilifries.setText("$" + priceval2);

        }
    });

    count3 = 1;
    priceval3 = 2;
    qtypotatofries = (TextView) findViewById(R.id.textView5);
    amt_potatofries = (TextView) findViewById(R.id.textView6);
    add3 = (Button) findViewById(R.id.button5);
    add3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count3++;
            qtypotatofries.setText("" + count3);
            priceval3 = priceval3 + 2;
            amt_potatofries.setText("$" + priceval3);

        }
    });
    sub3 = (Button) findViewById(R.id.button6);
    sub3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count3--;
            if (count3 < 1) {
                count3 = 1;
            }
            qtypotatofries.setText("" + count3);
            priceval3 = priceval3 - 2;
            if (priceval3 <= 2) {
                priceval3 = 2;
            }
            amt_potatofries.setText("$" + priceval3);

        }
    });
    count4 = 1;
    priceval4 = 3;
    qtygrilled = (TextView) findViewById(R.id.textView7);
    amt_grilled = (TextView) findViewById(R.id.textView8);
    add4 = (Button) findViewById(R.id.button7);
    add4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count4++;
            qtygrilled.setText("" + count4);
            priceval4 = priceval4 + 3;
            amt_grilled.setText("$" + priceval4);

        }
    });
    sub4 = (Button) findViewById(R.id.button8);
    sub4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            count4--;
            if (count4 < 1) {
                count4 = 1;
            }
            qtygrilled.setText("" + count4);
            priceval4 = priceval4 - 3;
            if (priceval4 <= 3) {
                priceval4 = 3;
            }
            amt_grilled.setText("$" + priceval4);

        }
    });

    order = (Button) findViewById(R.id.button9);

    order.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            double totalamount = 0;

            if (frenchfries.isChecked()) {
                totalamount += priceval1;
            }

            if (chilicheese.isChecked()) {
                totalamount += priceval2;
            }
            if (sweetpotato.isChecked()) {
                totalamount += priceval3;
            }
            if (grilledvegetables.isChecked()) {
                totalamount += priceval4;
            }
            total.setText("$" + totalamount);

        }
    });
}

这是我的第一堂课...我想将totalamount的值用于另一个类并在其他类中显示...我该怎么办?

private ImageButton ib, ib1;
private Calendar cal;
private int day;
private int month;
private int year;
private EditText et;
Button book;
private int hour;
private int min;
private EditText et1;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
TextView total1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.booktable1);
    /*
     * total1=(TextView) findViewById(R.id.textView2); long
     * textView10=getIntent().getLongExtra("total2", 0); String
     * str="Total : "+textView10; total1.setText(str);
     */
    // mDateButton = (Button) findViewById(R.id.date_button);
    ib = (ImageButton) findViewById(R.id.imageButton1);
    ib1 = (ImageButton) findViewById(R.id.imageButton2);
    cal = Calendar.getInstance();
    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
    et = (EditText) findViewById(R.id.editText);
    book = (Button) findViewById(R.id.bookbtn);

    et1 = (EditText) findViewById(R.id.editText1);
    hour = cal.get(Calendar.HOUR_OF_DAY);
    min = cal.get(Calendar.MINUTE);

    /*
     * total1=(TextView) findViewById(R.id.textView2);
     * total1.setText(getIntent().getExtras().getString("total2"));
     */
    ib.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            // Show the DatePickerDialog
            showDialog(DATE_DIALOG_ID);
        }
    });

    ib1.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            // Show the DatePickerDialog
            showDialog(TIME_DIALOG_ID);
        }
    });

    book.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (et1.getText().toString().equals("")
                    || et.getText().toString().equals("")) {
                new AlertDialog.Builder(Booktable.this)

                        .setTitle("Field is Empty")

                        .setMessage("Either Date or Time is missing")

                        .setNeutralButton("Ok",
                                new DialogInterface.OnClickListener() {

                                    public void onClick(
                                            DialogInterface dialog,

                                            int which) {

                                    }

                                }).show();

            } else {

                /*
                 * Intent intent = new Intent(Intent.ACTION_SENDTO,
                 * Uri.fromParts( "mailto","rupesh.mishra89@gmail.com",
                 * null)); intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
                 * intent.putExtra(Intent.EXTRA_TEXT, "message");
                 * startActivity(Intent.createChooser(intent,
                 * "Choose an Email client :"));
                 */

                Toast.makeText(getApplicationContext(), "Table is booked",
                        Toast.LENGTH_LONG).show();

            }
            ;

        }
    });
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, timePickerListener, hour, min,
                false);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        if ((selectedYear < year)
                || ((selectedMonth < month) && (selectedYear == year))
                || ((selectedDay < day) && (selectedYear == year) && (selectedMonth == month))) {
            new AlertDialog.Builder(Booktable.this)

            .setTitle("Wrong Input!")

            .setMessage("Please Enter Valid Date")

            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,

                int which) {

                }

            }).show();
        } else {
            et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                    + selectedYear);
        }
    }
};
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        int hour;
        String am_pm;

        if (hourOfDay > 12) {
            hour = hourOfDay - 12;
            am_pm = "PM";
        } else {
            hour = hourOfDay;
            am_pm = "AM";
        }

        et1.setText(hour + " : " + minute + " " + am_pm);

        if (hourOfDay < 10) {
            new AlertDialog.Builder(Booktable.this)

            .setTitle("Hotel not open")

            .setMessage("Hotel Timings: 10 AM to 12 PM")

            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,

                int which) {

                }

            }).show();
            et1.setText("");
        } else {
            et1.setText(hour + " : " + minute + " " + am_pm);
        }

    }
};

我想通过祝酒词显示总数......

4 个答案:

答案 0 :(得分:1)

在你的第一个Activity中,创建一个Intent,将包含数据的包放入其中,然后用这个意图启动第二个Activity:

Intent intent = new Intent(this, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putDouble("total", total);
intent.putExtras(bundle);
startActivity(intent);

在第二个活动的onCreate中,您可以从捆绑中获取数据并显示Toast。

double totat = getIntent().getExtras().getDouble("total");
Toast toast = Toast.makeText(this, "some text " + total, Toast.LENGTH_LONG);

答案 1 :(得分:0)

我会在第一个类中创建一个getter方法,比如

public double getTotalAmount()
{
    return totalamount;
}

然后在第二节课中,你需要创建对第一个类的引用,然后你想要Toast的位置,你可以拥有

Toast.makeText(this, *first class*.getTotalAmount(), Toast.LENGTH_SHORT).show();

答案 2 :(得分:0)

您需要使用intent.putextra。这样的事情应该有效:

Intent intent = new Intent(Activity1.this, Activity2.class);   
intent.putExtra("total", total);
startActivity(intent);

在你的activity2中尝试获得你之前的总数。

int total = getIntent().getExtras().getInt("total");

答案 3 :(得分:0)

您的课程是活动课程。

因此,将值从一个活动传递到另一个活动的最佳方法是使用Intent。 你可以这样想:

在发送类中,您将值放入Intend并开始第二个活动:

Intent i1 = new Intent(this, Your2Activity.class);
i1.putExtra("YourKey", YourValue);
startActivity(i1); 

在第二个活动中,您可以检索您的值:

Intent intent = getIntent();
String result = intent.getStringExtra("YourKey");

显示结果只是:

Toast.makeText(this, "Total: "+result, Toast.LENGTH_SHORT).show();