选择RadioGroup id并在单击按钮时对其进行操作

时间:2019-02-14 01:08:48

标签: android android-layout

我只是想让此页面根据您选中的框将我发送到另一页面。

它应该找到已选中框的ID。然后,根据ID,应该转到新页面。但是,当我单击按钮时,没有任何效果。我整夜都在尝试解决此问题。

RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sporgsmaltype);

    radiogroup = findViewById(R.id.radioGroup);

    Button button2 = findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int radioid = radiogroup.getCheckedRadioButtonId();
            if(radioid == 0) {
                openbought();
            }
            else if(radioid == 1) {
                openbought();
            }
            else if(radioid == 2) {
                openbought();
            }
        }


    });

}


        public void openservice() {
            Intent intentservice = new Intent(this, sporgsmal1.class);
            startActivity(intentservice);
        }
        public void openrepair() {
                Intent intentrepair = new Intent(this, sporgsmal1.class);
                startActivity(intentrepair);
            }

        public void openbought() {
            Intent intentbought = new Intent(this, detaljerMain.class);
            startActivity(intentbought);
        }

}

4 个答案:

答案 0 :(得分:1)

您需要检查

  

radiogroup.getCheckedRadioButtonId()

单选按钮ID不为0/1/2的单选按钮,这不是单选按钮ID。  你可以试试这个

RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sporgsmaltype);

radiogroup = findViewById(R.id.radioGroup);

Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int radioid = radiogroup.getCheckedRadioButtonId();
        if(radioid == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
            openbought();
        }
        else if(radioid == findViewById(R.id.YOUR_SECOND_RADIO_BUTTON).getId()) {
            openbought();
        }
        else if(radioid == findViewById(R.id.YOUR_THIRD_RADIO_BUTTON).getId()) {
            openbought();
        }
    }


});

}


    public void openservice() {
        Intent intentservice = new Intent(this, sporgsmal1.class);
        startActivity(intentservice);
    }
    public void openrepair() {
            Intent intentrepair = new Intent(this, sporgsmal1.class);
            startActivity(intentrepair);
        }

    public void openbought() {
        Intent intentbought = new Intent(this, detaljerMain.class);
        startActivity(intentbought);
    }
}

答案 1 :(得分:1)

RadioGroup radiogroup;
RadioButton serviceRadioButton,repairRadioButton,boughtRadioButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sporgsmaltype);

    radiogroup = findViewById(R.id.radioGroup);
    serviceRadioButton = radiogroup.findViewById(R.id.service_id);
    repairRadioButton = radiogroup.findViewById(R.id.repair_id);
    boughtRadioButton = radiogroup.findViewById(R.id.bought_id);

    Button button2 = findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int radioid = radiogroup.getCheckedRadioButtonId();
            if(radioid == R.id.service_id) {
                openservice(serviceRadioButton);
            }
            else if(radioid == R.id.repair_id) {
                openrepair(repairRadioButton);
            }
            else if(radioid == R.id.bought_id) {
                openbought(boughtRadioButton);
            }
        }


    });

}


        public void openservice() {
            Intent intentservice = new Intent(this, sporgsmal1.class);
            startActivity(intentservice);
        }
        public void openrepair() {
                Intent intentrepair = new Intent(this, sporgsmal1.class);
                startActivity(intentrepair);
            }

        public void openbought() {
            Intent intentbought = new Intent(this, detaljerMain.class);
            startActivity(intentbought);
        }
}

答案 2 :(得分:0)

尝试这样

RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sporgsmaltype);

    radiogroup = findViewById(R.id.radioGroup);

    Button button2 = findViewById( R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int radioid = radiogroup.getCheckedRadioButtonId();
            if(radioid ==  R.id.service_id) {
                openbought(v);
            }
            else if(radioid == R.id.repair_id) {
                openbought(v);
            }
            else if(radioid == R.id.bought_id) {
                openbought(v);
            }
        }

    });

}


public void openservice(View view) {
    Intent intentservice = new Intent( view.getContext(), sporgsmal1.class);
    startActivity(intentservice);
}
public void openrepair(View view) {
    Intent intentrepair = new Intent(view.getContext(), sporgsmal1.class);
    startActivity(intentrepair);
}

public void openbought(View view) {
    Intent intentbought = new Intent(view.getContext(), detaljerMain.class);
    startActivity(intentbought);
}
}

答案 3 :(得分:0)

谢谢,现在可以使用radiogroup.getCheckedRadioButtonId()