Domanda

I'm Developing an android app in which the Login activity contains 2 radiobuttons (Manager and Teamleader) and also a button Proceed.If the Manager radioButton is selected then the Manager Questionnaire activity as to open, if the teamleader radioButton is selected the teamleader Questionnaire must open.Can anyone please help me in modifying the java code. Thanks in Advance.

This is my login activity

public void OnClickListener(View v)
{
    final RadioButton manager = (RadioButton) findViewById(R.id.radioButton1);
  final RadioButton teamleader = (RadioButton) findViewById(R.id.radioButton2);

  Button proceed = (Button) findViewById(R.id.button1);
  proceed.setOnClickListener(new View.OnClickListener()
  {
        @Override
        public void onClick(View v) 
        {

        if(manager.isChecked())
        {
            Intent managerIntent = new Intent(getApplicationContext(), ManagerQuestionnaire1.class); 
            startActivityForResult(managerIntent, 0);
            }
            else
            {
              if(teamleader.isChecked())
              {
              Intent teamleaderIntent = new Intent(getApplicationContext(),                 TeamleaderQuestionnaire1.class);
              startActivityForResult(teamleaderIntent, 0);
              }
            }
        }
    });
}

And here is the Xml code for button (proceed)

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="OnClickListener"
        android:text="@string/Proceed_b1" />
È stato utile?

Soluzione

Give this a shot:

public void OnClickListener(View v)
{
    final RadioButton manager = (RadioButton) findViewById(R.id.radioButton1);
    final RadioButton teamleader = (RadioButton) findViewById(R.id.radioButton2);

    Button proceed = (Button) findViewById(R.id.button1);
    proceed.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {

        if(manager.isChecked()) {
            Intent managerIntent = new Intent(getApplicationContext(), ManagerQuestionnaire1.class); 
            startActivityForResult(managerIntent, 0);
        } else if (teamleader.isChecked()) {
              Intent teamleaderIntent = new Intent(getApplicationContext(),                 TeamleaderQuestionnaire1.class);
              startActivityForResult(teamleaderIntent, 0);
        }
    });
}

The problem with your code is that this:

if(teamleader.isChecked())

was nested inside the:

if(manager.isChecked())

Meaning it would only hit if the manager was checked to begin with!

Hope that helps, happy coding.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top