无法触发RadioGroup的onCheckedChanged函数

4
我的应用程序跟踪餐厅服务员的班次销售情况,以帮助他们预算。在显示过去班次的活动中,我在ListView下创建了一个RadioGroup,以便用户可以选择显示午餐、晚餐或两者都显示。
我在该活动中实现了RadioGroup.onCheckedChangeListener,但onCheckedChanged从未被调用。我还尝试使用匿名内部类作为监听器,结果相同。
我试图复制/修改来自此答案的代码:https://stackoverflow.com/a/9595528 ,但当我在回调函数中添加@Override时,Eclipse编译器给出了一个错误(不是警告),提示该方法必须覆盖超类,并且快速修复是删除override。
我很确定签名是匹配的,因为它们是使用Eclipse的自动完成和实现方法工具创建的。然后,我按照说明将我的java编译器从1.5移动到1.6,以上列出的任何行为似乎都没有改变。
以下是我认为与此相关的代码:
public class DataActivity extends ListActivity implements OnCheckedChangeListener{
    RadioButton rbBoth;
    RadioButton rbDinnerOnly;
    RadioButton rbLunchOnly;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.database);
        // ...
        final RadioGroup rgGroup = (RadioGroup)findViewById(R.id.DataRadioGroup);
        rbBoth = (RadioButton)findViewById(R.id.RadioBoth);
        rbDinnerOnly = (RadioButton)findViewById(R.id.RadioDinnerOnly);
        rbLunchOnly = (RadioButton)findViewById(R.id.RadioLunchOnly);
        rgGroup.setOnCheckedChangeListener(this);
        populateAllShifts();
    }

    // ...

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        rbLunchOnly.setText("Click!");
        Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show();
        if(group.getCheckedRadioButtonId() == R.id.RadioBoth){
            populateAllShifts();
            return;
        }
        if(group.getCheckedRadioButtonId() == R.id.RadioLunchOnly){
            populatLunchShifts();
            return;
        }
        if(group.getCheckedRadioButtonId() == R.id.RadioDinnerOnly){
            populateDinnerShifts();
            return;
        }
    }

    // ...

}

这个类中有一个带有自定义适配器的 ListView,但如果我的理解和我的 XML 正确的话,RadioGroup 应该在列表外:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llDataLayout"
    android:weightSum="5"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

    <ListView android:layout_weight="4"
        android:layout_width="fill_parent"
        android:id="@android:id/list"
        android:layout_height="wrap_content">
    </ListView>

    <RadioGroup 
        android:layout_weight="1"
        android:id="@+id/DataRadioGroup"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <RadioButton android:text="Lunch and Dinner"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RadioBoth"/>
        <RadioButton android:text="Dinner Only"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RadioDinnerOnly"/>
        <RadioButton android:text="Lunch Only"
            android:textSize="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RadioLunchOnly"/>

    </RadioGroup>

</LinearLayout>

有什么想法吗?

整理一下你的介绍 - 没有段落我几乎无法阅读它 - 除此之外是个好问题。 - nickhar
谢谢,nickhar!我现在正在修复它... - LeBeau
真令人沮丧!感觉像是打错字了,但我已经反复检查了这段代码好几次,却找不到错误。我有其他类似的代码在同一个应用程序中运行正常。难道是因为Activity扩展了ListActivity而不仅仅是Activity吗? - LeBeau
4个回答

1

您已经作为参数获取了checkedId,这是新选中的单选按钮的唯一标识符。

public void onCheckedChanged(RadioGroup group, int checkedId) {
    rbLunchOnly.setText("Click!");
    Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show();

    switch(checkedId){
    case R.id.RadioBoth :
        populateAllShifts();
        break;

    //--other cases---

   }

1
我并不认为这解决了我的问题,但它确实让我的代码更加简洁。谢谢! - LeBeau

1

找到了问题,但答案并没有从我在问题中提供的信息中显而易见。问题是 cruft。

这起初是我的第一个项目,在几个月前试图笨拙地解决数据库问题时,我已经不必要地覆盖了 onStart() 和 onRestart(),并在 onCreate() 中使用了几乎相同的代码。随着时间的推移,onCreate 已经发生了变化,但那些方法没有。一旦我删除它们,代码就完美地工作了。

我没有在问题中展示这些方法。抱歉!

感谢大家的帮助!现在我该怎么做?删除这个问题吗?


0

你应该使用这个方法:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
{
    if(isChecked == true)
    {           
        if(buttonView == radioA)            
            tvInfo.setText("A");
        else if(buttonView == radioB)               
            tvInfo.setText("B");        
        else if(buttonView == radioC)           
            tvInfo.setText("C");
    }
}

谢谢您的建议,但不能行。我添加了这个函数,并将“implements”更改为符合其签名,并将所有三个按钮的监听器设置为'this',但仍然没有调用onCheckedChanged。 - LeBeau

0

应该可以在你的XML中工作。你可以通过android:clickable="true"为单选按钮提供可点击选项。 在Java中...

public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch(checkedId){
    case R.id.RadioBoth :
        populateAllShifts();
        break;
case R.id.RadioDinnerOnly:
        populatDinnerShifts();
        break;
    //-----
default:

break;
   }

谢谢!但我已经将“可点击”选项添加到所有三个按钮上,但onCheckedChanged()仍然从未被调用。 - LeBeau
1
还有一件事,你应该在布局中勾选一个默认的单选按钮。 - S.D.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接