简体   繁体   English

选择单选组时未启用按钮

[英]Button not enabled when radio group are selected

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        Dbb db=new Dbb(this);
        quesll=db.getAllQuestions();
        currentQ=quesList.get(qid);
        txtresult=(TextView)findViewById(R.id.textView2);
        txtQuestion=(TextView)findViewById(R.id.textView1);
        radiobuttona=(RadioButton)findViewById(R.id.radio0);
        radiobuttonb=(RadioButton)findViewById(R.id.radio1);
        buttonnext=(Button)findViewById(R.id.button1);
        buttonnext.setEnabled(false);
        setQuestionView();
        buttonnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup group = (RadioGroup) findViewById(radioGroup);
                RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
               if (group .isSelected()){
                   butNext.setEnabled(true);
               }

Why button not set enabled, when radio group are selected? 选择无线电组后,为什么未启用按钮设置?

This code will work!! 该代码将起作用!

public class Sample extends Activity {

    private RadioGroup radioGroup;
    private RadioButton radioButtonmale, radioButtonfemale;
    private Button btnDisplay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stackover);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        radioGroup = (RadioGroup) findViewById(R.id.radioSex);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
        btnDisplay.setEnabled(false);
        radioButtonmale = (RadioButton) findViewById(R.id.radioMale);
        radioButtonfemale = (RadioButton) findViewById(R.id.radioFemale);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int id = radioGroup.getCheckedRadioButtonId();
                View radioButton = radioGroup.findViewById(id);
                if (radioButton.getId() == R.id.radioMale) {
                    btnDisplay.setEnabled(true);
                } else {
                    btnDisplay.setEnabled(true);
                }
            }
        });

    }
}

I think the problem is here: 我认为问题出在这里:

butNext.setEnabled(true);

You don't have a button called "butNext". 您没有名为“ butNext”的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何隐藏下一个按钮并在选择单选按钮时显示 - How to hide next button and show when radio group button is selected 在按钮组中选择了哪个单选按钮 - Which radio button is selected in a button group 如何检查每个无线电组中的按钮是否被选中? - How to check is the button is selected in each radio group? 从按钮组中获取所选单选按钮的名称 - get the name of the selected radio button from button group 未选择单选按钮时调用ActionEvent - Having ActionEvent called when Radio Button is not selected 检索从组中选择了哪个“摇摆”单选按钮 - retrieving what swing radio button is selected from a group 单选按钮使单选按钮外圈的颜色与选择单选按钮时的颜色不同 - radio button make radio button outer circle a different colour to the colour when the radio button is selected 选中时,Android Studio单选按钮未显示为选中状态 - Android Studio Radio Button not read as selected when selected 选择/不选择单选按钮时如何隐藏文本字段? - How to hide a Text Field when a radio button is selected/not selected? 如何在核心java程序中添加单选按钮组,以便一次只选择一个单选按钮? - How to add a radio button group in a core java program such that only one radio button is selected at one time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM