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

選擇無線電組后,為什么未啟用按鈕設置?

該代碼將起作用!

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

    }
}

我認為問題出在這里:

butNext.setEnabled(true);

您沒有名為“ butNext”的按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM