I apologise if an answer to this has already been posted, but I have looked for ages and can't find anything suitable.
I have four radio buttons and want to process a different calculation depending on which one has been selected from input data (from edit text). I also have a button to press to start the calculation.
The whole process works apart from the radio buttons. I know I need to use a switch/case command, but i am not sure ow to set it up.
I have tried the following in the main activity java class:
package com.minichanic.idealgas;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity implements android.view.View.OnClickListener{
Button add, subtract, multiply, divide, mod;
EditText pressin, volin, molin, tempin;
RadioButton radio0, radio1, radio2, radio3;
RadioGroup RadioGroup1;
private int variable;
/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
// Reference TextViews and Buttons
pressin = (EditText) findViewById(R.id.editText1);
volin = (EditText) findViewById(R.id.editText2);
molin = (EditText) findViewById(R.id.editText3);
tempin = (EditText) findViewById(R.id.editText4);
add = (Button) findViewById(R.id.button1);
// Set listeners for when buttons are pressed
add.setOnClickListener(this);
}
/**
* Switch statement to decide which button was pressed droidacid.com
*/
public void onClick(View arg0) {
// Get values from EditTexts
double pressure = Double.parseDouble(pressin.getText().toString());
double volume = Double.parseDouble(volin.getText().toString());
double moles = Double.parseDouble(molin.getText().toString());
double temperature = Double.parseDouble(tempin.getText().toString());
// Perform relevant operations depending on radio button
// Check which radio button was clicked
switch(variable){
case R.id.radio0:
pressure = ((moles * 8.31* temperature)/volume);
break;
case R.id.radio1:
volume = ((moles * 8.31* temperature)/pressure);
break;
case R.id.radio2:
moles = ((pressure * volume) / (temperature * 8.31));
break;
case R.id.radio3:
temperature = ((pressure * volume) / (moles * 8.31));
break;
}
// Add result to Running total stored in output TextView
String tempresult = ""+temperature;
TextView tresult = (EditText) findViewById(R.id.editText4);
tresult.setText(tempresult);
String pressresult = ""+pressure;
TextView presult = (EditText) findViewById(R.id.editText1);
presult.setText(pressresult);
String molresult = ""+moles;
TextView mresult = (EditText) findViewById(R.id.editText3);
mresult.setText(molresult);
String volresult = ""+volume;
TextView vresult = (EditText) findViewById(R.id.editText2);
vresult.setText(volresult);
}
}
The xml layout contains:
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/pressure" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vol" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mol" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/temp" />
</RadioGroup>
It was working fine, until I started introduced the radio buttons and now it crashes. Many thanks for any ideas.