【问题标题】:Android Radio Button with Submit Not working带有提交的Android单选按钮不起作用
【发布时间】:2016-08-30 18:32:59
【问题描述】:

试图让我的单选按钮工作,它们在一个组中,因此一次只能选择 1 个,但我想允许选择而不是触发选项,而是提交按钮收集结果,然后调用正确的方法这是我到目前为止所拥有的:

    testRm.setOnClickListener(new Button.OnClickListener(
    ) {
        @Override
        public void onClick(View v) {
            setReminder();
            displayToast("Test");
        }
    });

setReminder 方法

public void setReminder() {
    onewk = (RadioButton) findViewById(R.id.radioButton);
    twowk = (RadioButton) findViewById(R.id.radioButton2);
    onemnt = (RadioButton) findViewById(R.id.radioButton3);
    mag = (RadioButton) findViewById(R.id.radioButton4);
    twoprt = (RadioButton) findViewById(R.id.radioButton5);

    if(onewk.isSelected()) {
        scheduleNotification(getNotification("1 Week"), 7);
    }
    if(twowk.isSelected()) {
        scheduleNotification(getNotification("2 Weeks"), 14);
    }
    if(onemnt.isSelected()) {
        scheduleNotification(getNotification("1 Month"), 30);
    }
    if(mag.isSelected()) {
        scheduleNotification(getNotification("Test 1"), 30);
    }
    if(twoprt.isSelected()) {
        scheduleNotification(getNotification("Test 2"), 1);
    }
}

吐司法

public void displayToast(String x) {
    g1 = (RadioGroup) findViewById(R.id.rg1);
    g2 = (RadioGroup) findViewById(R.id.rg2);
    days = (EditText) findViewById(R.id.editText);

    Toast.makeText(reminders.this, x, Toast.LENGTH_LONG).show();
    g1.clearCheck(); g2.clearCheck(); days.setText("");
}

我可以在网上找到的唯一示例是在 onEvent 方法中使用 switch 语句,因此我尝试了其他方法但失败得很惨,因为从未调用 scheduleNotification 方法。

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    对于RadioButton,你需要检查它是checked还是not,意思是这样的

    onewk.isChecked() 而不是onewk.isSelected()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2015-08-05
      • 2015-10-10
      • 2016-09-18
      • 1970-01-01
      • 2018-10-08
      相关资源
      最近更新 更多