java - 选择单选按钮组时按钮未启用

标签 java android android-studio

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

当选择单选组时,为什么按钮未设置为启用?

最佳答案

这段代码可以工作!!

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

    }
}

关于java - 选择单选按钮组时按钮未启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40389731/

相关文章:

java - android studio 在哪里找到这个代理?

java - tools.jar 似乎不在 Windows 8 上的 Android Studio 类路径中

java - 如何为 IntelliJ IDEA 中的所有项目设置默认项目位置?

JAVA - 使用 Box.createGlue() 将对象放置在屏幕底部?

android - 视频 View 不播放 youtube 视频

java - MIUI 中的 Internet 连接状态被阻止

android - 如何从 Android LiveData<List<T>> 获取简单的 List<T>?

java - 移动 2D ArrayList 中的元素

java - 新版本的 Netbeans 刚刚问世。我能做些什么来避免再次手动搜索我的插件吗?

java - 表达一个通用的 `MethodType` 返回类型为任何对象