basic android: syntax for switch statement instead of else-if

13,494

Solution 1

What the people above me said is correct, but for the sake of using a switch statement for the hell of it, you could set an OnCheckedChangedListener on your RadioGroup, then use a class like this:

private class MyCheckedChangedListener implements OnCheckedChangeListener {

@Override
public void onCheckedChanged( RadioGroup group, int checkedId ) {

    switch (checkedId) {
        case R.id.radio15:
            percentage = 15f;
            break;
        case R.id.radio18:
            percentage = 18f;
            break;
        case R.id.radio20:
            percentage = 20f;
            break;
    }
}

}

Solution 2

A switch is used on one variable - i.e. you have x, which can equal 3,5 or 7. Then you switch x and give several cases - what to do on each possible value (you can also have a default case, when none of the given values match). In your case, you're checking several different variables, so if ... else if ... else is the correct method. You can, of course, make the radio boxes set a shared variable, which you can then switch.

Share:
13,494
untriangulated
Author by

untriangulated

Updated on June 04, 2022

Comments

  • untriangulated
    untriangulated almost 2 years

    I am working on an android beginner's tutorial that is a tip calculator. It runs properly, but I was wondering how to replace the else-if statement with a switch statement. Not that it is that important for the purposes of this program, but I'm just trying to wrap my mind around the syntax.

    package com.android.tipcalc;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.view.View;
    
    public class tipcalc extends Activity
    {
    
        private EditText txtbillamount;
        private EditText txtpeople;
        private RadioGroup radiopercentage;
        private RadioButton radio15;
        private RadioButton radio18;
        private RadioButton radio20;
    
        private TextView txtperperson;
        private TextView txttipamount;
        private TextView txttotal;
    
        private Button btncalculate;
        private Button btnreset;
    
        private double billamount = 0;
        private double percentage = 0;
        private double numofpeople = 0; 
        private double tipamount = 0;
        private double totaltopay = 0;
        private double perperson = 0; 
    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            initControls();
    
        }
    
        private void initControls()
        {
            txtbillamount = (EditText)findViewById(R.id.txtbillamount);
            txtpeople = (EditText)findViewById(R.id.txtpeople);
            radiopercentage = (RadioGroup)findViewById(R.id.radiopercentage);
            radio15 = (RadioButton)findViewById(R.id.radio15);
            radio18 = (RadioButton)findViewById(R.id.radio18);
            radio20 = (RadioButton)findViewById(R.id.radio20);
            txttipamount=(TextView)findViewById(R.id.txttipamount);
            txttotal=(TextView)findViewById(R.id.txttotal);
            txtperperson=(TextView)findViewById(R.id.txtperperson);
    
            btncalculate = (Button)findViewById(R.id.btncalculate);
            btnreset = (Button)findViewById(R.id.btnreset);
    
            btncalculate.setOnClickListener(new Button.OnClickListener() {
                public void onClick (View v){ calculate(); }});
            btnreset.setOnClickListener(new Button.OnClickListener() {
                public void onClick (View v){ reset(); }});
    
        }
    
        private void calculate()
        {
        billamount=Double.parseDouble(txtbillamount.getText().toString());
        numofpeople=Double.parseDouble(txtpeople.getText().toString());
    
        if (radio15.isChecked()) {
            percentage = 15.00;
        } else if (radio18.isChecked()) {
            percentage = 18.00;
        } else if (radio20.isChecked()) {
            percentage = 20.00;
        }
    
        tipamount=(billamount*percentage)/100;
        totaltopay=billamount+tipamount;
        perperson=totaltopay/numofpeople;
    
        txttipamount.setText(Double.toString(tipamount));
        txttotal.setText(Double.toString(totaltopay));
        txtperperson.setText(Double.toString(perperson));        
        }
    
        private void reset()
        {
        txtbillamount.setText("");
        txtpeople.setText("");
        radiopercentage.clearCheck();
        radiopercentage.check(R.id.radio15);
        txttipamount.setText("...");
        txttotal.setText("...");
        txtperperson.setText("...");
        }
    }