3 Replies - 1165 Views - Last Post: 11 May 2013 - 03:50 PM

#1 coolbud012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 01-July 11

Selecting radio buttons and perform action according to that

Posted 09 May 2013 - 01:57 PM

Hey guys I am thinking and trying over this problem since last 1 day. Searched a lot too but didnt get what I am exactly trying to do.

Ok let me tell you. I am trying to make an app, where the app will do some calculations based on the user inputs ( like in a calculator)

Firstly I have created a radio group with 2 radiobuttons.

Then after that 3 text boxes and then atlast a button.

Now what I am trying to implement with java is :

When the user select 1st radio button it should only display 2 text fields and when user selects 2nd radio button it should display 3 text fields.

And after that when the user inputs the required values and click on calculate the it should work according to that input values.

Means I will be having different formulas implemented by onclicklistener by button.

now let me explain whole thing by taking an example.

When 1st radio button is selected

Suppose you start an app and 1st radio button is selected. And 2 text fields are there. Now user inputs the values and click calculate and it should display the "Sum" of both numbers.


When 2nd radio button is selected

Now the user selects 2nd radio button and 3 text fields will be displayed. User enters value in all three and click calculate and it should display multiplication of all 3 numbers.



Guys I am trying to implement this thing since last 1 day or more but getting more and more confused of exactly what to do. How to hide the 3rd text field if 1st radio button is selected and how to implement 2 different formulas when calculate button is pressed depending on the radio button and text fields...Please help me.

Dont think that I am asking for code. I just need your suggesions and advices on how to implement this thing.

Please dont rate this question as negative as I'm posting this question after trying everything I could.

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Selecting radio buttons and perform action according to that

#2 coolbud012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 01-July 11

Re: Selecting radio buttons and perform action according to that

Posted 09 May 2013 - 02:59 PM

This is what I have tried till now :

  package com.droidacid.apticalc;

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.View;
    import android.view.View.onclickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;

    public class timeNWork extends Activity implements onclickListener,         OnCheckedChangeListener{

	TextView TVnotice, TVoutput;
	RadioGroup RGselect;
	EditText num1, num2, num3;
	Button calculate;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aptitimenwork);
		initialize();
	}	
	
		private void initialize() 
		{
		
		TVoutput = (TextView) findViewById(R.id.TVresult);
		//RGselect = (RadioGroup) findViewById(R.id.rgSelect);
		
		num1 = (EditText) findViewById(R.id.ETfirst);
		num2 = (EditText) findViewById(R.id.ETsecond);
		num3 = (EditText) findViewById(R.id.ETthird);
		RGselect.setOnCheckedChangeListener(this);
		
		}

	@Override
	public void onclick(View v) {
		// TODO Auto-generated method stub
		
		Double numb1 = Double.parseDouble(num1.getText().toString());
		Double numb2 = Double.parseDouble(num2.getText().toString());
		Double numb3 = Double.parseDouble(num3.getText().toString());
		
		double result = 0;
		
		if(RGselect.getId()==R.id.rbtwoperson)
	      {
			num3.setVisibility(2);
			double number1 = numb1;
			double number2 = numb2;
			result = 100 / (number1 + number2);
			TVoutput.setText(Double.toString(result));
	      }
		else
		{
			double number1 = numb1;
			double number2 = numb2;
			double number3 = numb3;
			result = 100 / (number1 + number2 + number3);
			TVoutput.setText(Double.toString(result));
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		
		switch(arg1)
		{
		case R.id.rbtwo:
			num3.setEnabled(false);
            num3.setInputType(InputType.TYPE_NULL);
            num3.setFocusableInTouchMode(false);
            num3.clearFocus();
			break;
		case R.id.rbthree:
			
			break;
		}
		
	}

    }

Was This Post Helpful? 0
  • +
  • -

#3 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 135
  • View blog
  • Posts: 511
  • Joined: 14-August 09

Re: Selecting radio buttons and perform action according to that

Posted 10 May 2013 - 11:59 AM

For your radio button group you need to code for both cases. So if a user clicks the radio button to make the 3rd EditText be hidden, then clicks the radio button to make it come back, it will come back. I can't tell exactly what your problem is but that is what I am seeing at first glance. May need to see your layout xml too.
Was This Post Helpful? 0
  • +
  • -

#4 coolbud012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 01-July 11

Re: Selecting radio buttons and perform action according to that

Posted 11 May 2013 - 03:50 PM

Yeah did that and its working...thanks a lot :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1