如何根据选择的单选按钮打开特定活动?

时间:2014-03-05 18:48:18

标签: java android android-intent radio-button onclicklistener

我正在开发一个Android应用程序,其中Login活动包含2个radiobuttons(Manager和Teamleader)以及一个按钮Proceed。如果选择了Manager radioButton,那么Manager调查问卷活动将打开,如果选择了teamleader radioButton团队负责人问卷必须打开。任何人都可以帮我修改java代码。 在此先感谢。

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);
              }
            }
        }
    });
}

这是按钮的Xml代码(继续)

 <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" />

1 个答案:

答案 0 :(得分:1)

试一试:

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);
        }
    });
}

您的代码存在的问题是:

if(teamleader.isChecked())

嵌套在:

if(manager.isChecked())

意味着只有在经理被检查开始时它才会被击中!

希望有帮助,快乐编码。