计算并将数字传递给另一个活动

时间:2016-03-11 16:20:34

标签: android-studio android-activity radio-button calculator

我要在Android Studio中制作一个预订假期的应用程序。我要显示客户假期的价格取决于他们选择的住宿类型(3个单选按钮)和他们住的晚数(旋转器)。我有很多尝试进行计算而且我已经有了并且虽然它似乎没有问题,但是计算结果并没有传递给我的其他活动让我显示它。有人能帮助我吗?

MakeBooking活动

public class MakeBooking extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    Button hButton;
    Button reviewButton;
    Spinner parkSpin, nightSpin, adultSpin, sixteenSpin, fiveSpin;
    String radioAButtonSelected = "";
    String radioPButtonSelected = "";
     EditText fromDate;
     DatePickerDialog fromDatePickerDialog;
    SimpleDateFormat dateFormatter;
     float cabinPrice = 45;
   float caravanPrice = 25;
     float tentPrice = 15;
   float Price, Price1;

    private static final String[] parks={" ","Lothianside Camping and Caravanning Park", "Loch Bannoch Touring Park", "Mckeowns Holiday Park"};
    String chosenPark ="not chosen";
    private static final String[] nights={" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14" };
    String chosenNights = "not chosen";
    private static final String[] adults={" ", "0", "1", "2", "3", "4", "5" };
    String chosenAdults = "not chosen";
    private static final String[] under16={" ", "0", "1", "2", "3", "4", "5" };
    String chosenSixteen = "not chosen";
    private static final String[] under5={" ", "0","1", "2", "3", "4", "5" };
    String chosenFive = "not chosen";

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

        parkSpin= (Spinner) findViewById(R.id.parkSpinner);
        parkSpin.setOnItemSelectedListener(this);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                parks);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        parkSpin.setAdapter(aa);

        nightSpin = (Spinner)  findViewById(R.id.nightSpinner);
        nightSpin.setOnItemSelectedListener(this);
       aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                nights);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       nightSpin.setAdapter(aa);

        adultSpin = (Spinner)  findViewById(R.id.adultSpinner);
        adultSpin.setOnItemSelectedListener(this);
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                adults);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adultSpin.setAdapter(aa);


       sixteenSpin = (Spinner)  findViewById(R.id.underSSpinner);
        sixteenSpin.setOnItemSelectedListener(this);
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
               under16);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sixteenSpin.setAdapter(aa);

        fiveSpin = (Spinner)  findViewById(R.id.underFSpinner);
        fiveSpin.setOnItemSelectedListener(this);
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                under5);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fiveSpin.setAdapter(aa);



        hButton = (Button)findViewById(R.id.homeButton);
        hButton.setOnClickListener(this);
        reviewButton = (Button)findViewById(R.id.bookButton);
        reviewButton.setOnClickListener(this);

        dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.UK);


        findViewsById();

        setDateTimeField();



    }




    private void findViewsById() {
        fromDate = (EditText) findViewById(R.id.fromDate);

        fromDate.setInputType(InputType.TYPE_NULL);
        fromDate.requestFocus();

    }

    private void setDateTimeField() {
        fromDate.setOnClickListener(this);

        Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                fromDate.setText(dateFormatter.format(newDate.getTime()));
            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));





    }

    public void onAccommodationClicked(View v) {
        boolean checked = ((RadioButton) v).isChecked();
        switch (v.getId()) {
            case R.id.cabinRButton:
                if (checked)
                    radioAButtonSelected = "Cabin";
                Price = getCabinPrice(cabinPrice);
                break;
            case R.id.caravanRButton:
                if (checked)
                    radioAButtonSelected = "Caravan";
                Price = getCaravanPrice(caravanPrice);
                break;
            case R.id.tentRButton:
                if (checked)
                    radioAButtonSelected ="Tent";
                Price = getTentPrice(tentPrice);
                break;

        }
    }
    private float getCabinPrice(float cabinPrice) {
        return (Integer.parseInt(chosenNights) * cabinPrice);
    }

    private float getCaravanPrice(float caravanPrice) {
        return (Integer.parseInt(chosenNights) * caravanPrice);
    }

    private float getTentPrice(float tentPrice) {
        return (Integer.parseInt(chosenNights) * tentPrice);
    }

    public void onPetClicked(View v) {
        boolean checked = ((RadioButton) v).isChecked();
        switch (v.getId()) {
            case R.id.yesRButton:
                if (checked)
                    radioPButtonSelected = "Yes";
                break;
            case R.id.noRButton:
                if (checked)
                    radioPButtonSelected = "No";
                break;


        }
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        // Toast.makeText(this, "clicked." + parent.getId(), Toast.LENGTH_LONG).show();
        switch (parent.getId()) {
            case R.id.parkSpinner:
                chosenPark = parks[position];
                break;
            case R.id.nightSpinner:
                chosenNights = nights[position];
                break;
            case R.id.adultSpinner:
                chosenAdults = adults[position];
                if (chosenAdults == "0"){
                    Toast.makeText(this, "Please confirm an adult over 21 is in your party.", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.underSSpinner:
                chosenSixteen = under16[position];
                break;
            case R.id.underFSpinner:
                chosenFive = under5[position];
                break;
        }
    }


    public void onNothingSelected(AdapterView<?> parent) {
        chosenPark = "";
        chosenNights = "";
        chosenAdults = "";
       chosenSixteen = "";
      chosenFive = "";
    }


    public void onClick(View view) {
        Intent intent;

        switch (view.getId()) {
            case R.id.homeButton:
                intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.bookButton:
                intent = new Intent(this, ReviewBooking.class);
                startActivity(intent);
                break;
        }

        if(view == fromDate) {
            fromDatePickerDialog.show();
        }
        else {
            intent = new Intent(this, ReviewBooking.class);
            intent.putExtra("chosenPark", chosenPark);
            intent.putExtra("chosenNights", chosenNights);
            intent.putExtra("chosenAdults", chosenAdults);
            intent.putExtra("chosenSixteen", chosenSixteen);
            intent.putExtra("chosenFive", chosenFive);
            intent.putExtra("radioButtonSelected", radioAButtonSelected);
            intent.putExtra("radioPButtonSelected", radioPButtonSelected);
            intent.putExtra("fromDate", fromDate.toString());
            intent.putExtra("Price1", Price1);


            startActivity(intent);


        }
    }


}

评论预订活动

public class ReviewBooking extends Activity implements View.OnClickListener {
    Button hButton;
    float Price1;
    float cabin, caravan, tent;
    TextView radioChoice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_booking);
        hButton = (Button) findViewById(R.id.homeButton);
        hButton.setOnClickListener(this);

        Intent intent = getIntent();
        String uir = intent.getStringExtra("chosenPark");
        String uir2 = intent.getStringExtra("chosenNights");
        String uir3 = intent.getStringExtra("chosenAdults");
        String uir4 = intent.getStringExtra("chosenSixteen");
        String uir5 = intent.getStringExtra("chosenFive");
        String uir6 = intent.getStringExtra("radioAButtonSelected");
        String uir7 = intent.getStringExtra("radioPButtonSelected");
        String uir8 = intent.getStringExtra("fromDate");
        String uir9 = intent.getStringExtra("Price1");


        TextView textView = (TextView) findViewById(R.id.parkChoice);
        textView.setText(uir);

        TextView textView2 = (TextView) findViewById(R.id.nightsChoice);
        textView2.setText(uir2);

        TextView textView3 = (TextView) findViewById(R.id.adultChoice);
        textView3.setText(uir3);

        TextView textView4 = (TextView) findViewById(R.id.sixteenChoice);
        textView4.setText(uir4);

        TextView textView5 = (TextView) findViewById(R.id.fiveChoice);
        textView5.setText(uir5);

        TextView radioChoice = (TextView) findViewById(R.id.radioChoice);
        radioChoice.setText(uir6);

        TextView petChoice = (TextView) findViewById(R.id.petChoice);
        petChoice.setText(uir7);

        TextView dateChoice = (TextView) findViewById(R.id.dateChoice);
        dateChoice.setText(uir8);

        TextView txtPrice = (TextView) findViewById(R.id.txtPrice);
        txtPrice.setText(uir9);

    }

    public void onClick(View view) {
        Intent intent;
        intent = new Intent(this, MainActivity.class);
        startActivity(intent);


    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用intent.putExtraString来传递字符串。和类似的方法也可以传递整数long double。