java - 如何根据选择的单选按钮打开特定 Activity ?

标签 java android android-intent radio-button onclicklistener

我正在开发一个 Android 应用程序,其中“登录” Activity 包含 2 个单选按钮(经理和团队负责人)以及一个“继续”按钮。如果选择了经理单选按钮,则如果选择了团队负责人单选按钮,则将打开经理调查问卷 Activity 团队领导问卷必须打开。任何人都可以帮我修改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" />

最佳答案

试一下:

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())

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

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

关于java - 如何根据选择的单选按钮打开特定 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22206563/

相关文章:

java - 将整数转换为浮点型

java - java中的引用

AndroidPlot 设置网格间距

java - 如何在 Android 中通过 MediaStore API 检索和打开保存到下载的 PDF 文件?

android - WebView 下载会在返回 WebView 之前短暂打开浏览器窗口

android - "IntentReceiver components are not allowed to register to receive intents"尝试确定电池电量时

java - 为什么 Java 让你强制转换为集合?

java - sql server 索引超出范围错误

java - 如何让Android上的Activity始终位于最前面?

java - Android - 而不是启动新类(class)/Activity 的新 Intent ,如何返回打开 Activity/类(class)?