首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RadioGroup onCheckedChanged函数不会触发

RadioGroup onCheckedChanged函数不会触发
EN

Stack Overflow用户
提问于 2012-10-19 11:22:03
回答 4查看 6.1K关注 0票数 4

我的应用程序会跟踪餐厅服务生的轮班销售额,以帮助他们进行预算。在显示过去班次的活动中,我在ListView下创建了一个RadioGroup,以便用户可以选择显示午餐、晚餐或两者都显示。

我已经在activity RadioGroup.onCheckedChangeListener中实现了,但是onCheckedChanged从未被调用过。我还尝试使用匿名内部类作为listener,同样的结果。

我尝试从以下答案复制/修改代码:https://stackoverflow.com/a/9595528 ...but当我将@Override添加到回调函数时,Eclipse编译器给我一个错误(而不是警告),该方法必须覆盖超类,而快速修复方法是删除覆盖。

我非常确定签名是匹配的,因为它们是使用Eclipse的自动完成和实现方法工具生成的。然后,我按照说明将java编译器从1.5迁移到1.6,上面列出的行为似乎都没有改变。

下面是我认为相关的代码:

代码语言:javascript
复制
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应该在列表之外:

代码语言:javascript
复制
<?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>

有什么想法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-22 02:19:28

发现了问题,但从我在问题中给出的信息看不到答案。问题就出在这里。

这是从我的第一个项目开始的,几个月前我笨手笨脚地试图解决一个数据库问题,我在onCreate()中用几乎相同的代码重写了onStart()和onRestart()。随着时间的推移,onCreate已经改变了,而这些方法并没有改变。一旦我删除了它们,代码就完美地工作了。

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

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

票数 1
EN

Stack Overflow用户

发布于 2012-10-19 12:47:05

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-10-19 11:29:48

您应该使用此方法:

代码语言:javascript
复制
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");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12966907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档