【问题标题】:RadioGroup onCheckedChanged function won't fireRadioGroup onCheckedChanged 函数不会触发
【发布时间】:2012-10-19 03:22:02
【问题描述】:

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

我已经在活动 RadioGroup.onCheckedChangeListener 中实现了,但永远不会调用 onCheckedChanged。我还尝试使用匿名内部类作为侦听器,结果相同。

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

我很确定签名是匹配的,因为它们是使用 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>

有什么想法吗?

【问题讨论】:

  • 整理你的介绍 - 没有段落我几乎无法阅读 - 否则很好的问题。
  • 谢谢,尼克哈!现在修复...
  • 太令人沮丧了!这感觉像是一个错字,但我已经一遍又一遍地查看这段代码,但找不到它。我有其他类似的代码可以在同一个应用程序中使用。可能是因为 Activity 扩展了 ListActivity 而不仅仅是 Activity?

标签: android android-widget


【解决方案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---

   }

【讨论】:

  • 我看不出这能解决我的问题,但它确实让我的代码更干净。谢谢!
【解决方案2】:

发现了问题,但从我在问题中提供的信息中看不到答案。问题很棘手。

这从我的第一个项目开始,几个月前在尝试解决数据库问题时,我在 onCreate() 中使用几乎相同的代码额外覆盖了 onStart() 和 onRestart()。 onCreate 随着时间的推移发生了变化,而那些方法没有。一旦我删除它们,代码就可以完美运行。

我没有在问题中展示这些方法。对不起!

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

【讨论】:

    【解决方案3】:

    你应该使用这个方法:

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

    【讨论】:

    • 感谢您的建议,但没有骰子。我添加了这个函数,更改了“实现”以匹配其签名,并将所有三个按钮的侦听器设置为“this”,但仍然永远不会调用 onCheckedChanged。
    【解决方案4】:

    它应该可以工作.. 在你的 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()。
    • 另外,您应该在布局中选中一个默认单选按钮。
    猜你喜欢
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多