【问题标题】:how to open a particular activity based on which radiobutton is selected?如何根据选择的单选按钮打开特定活动?
【发布时间】:2014-03-06 03:22:03
【问题描述】:

我正在开发一个 android 应用程序,其中登录活动包含 2 个单选按钮(经理和团队领导)以及一个按钮继续。如果选择了经理单选按钮,那么经理问卷活动将打开,如果团队领导单选按钮被选择团队负责人问卷必须打开。任何人都可以帮我修改java代码。 提前致谢。

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

【问题讨论】:

  • 当你完全按下继续时会发生什么?
  • 问题是无论按下哪个单选按钮,ManagerQuestionnaire Activity 只会打开。

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


【解决方案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())

意味着只有在开始检查经理时才会生效!

希望对您有所帮助,祝您编码愉快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2014-03-02
    • 2013-03-04
    相关资源
    最近更新 更多