【问题标题】:In android, onCheckedChanged of a Radiogroup is not triggered when I Programmatically set a Radiobutton by call setChecked(true)在 android 中,当我通过调用 setChecked(true) 以编程方式设置 Radiobutton 时,不会触发 Radiogroup 的 onCheckedChanged
【发布时间】:2012-09-07 02:10:04
【问题描述】:

在我的应用程序中,我需要以编程方式设置一个单选按钮并期望触发 Radiogroup 的 onCheckedChanged,以便我可以在其中执行某些操作。

以编程方式设置单选按钮的代码。

 LayoutInflater inflater = LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
 View view = inflater.inflate(R.layout.footer, null);
 RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
 btn.setChecked(true);

在另一个活动中,我做了类似的事情:

 public class MainActivity extends ActivityGroup implements RadioGroup.OnCheckedChangeListener{
      protected void onCreate(Bundle savedInstanceState){
            mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
            mRadioGroup.setOnCheckedChangeListener(this);
      }

      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId){
            RadioButton btn = (RadioButton)findViewById(checkedId);
            if(btn != null){
                  do_something();
            }
      }
  }

但是,看起来这种方式行不通,从未调用过 onCheckedChanged。有谁知道我应该怎么做?谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    在初始化时触发默认的单选复选框。使用 clearCheck 方法将所有内容设置为未选中。然后,设置处理程序,最后设置默认值,您的处理程序将触发。

    itemtypeGroup.clearCheck();
    

    然后,像往常一样添加一个监听器:

    mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
                mRadioGroup.setOnCheckedChangeListener(this);
                mRadioGroup.clearCheck();
    
    RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
    btn.setChecked(true);
    

    【讨论】:

    • 如果您在 xml 中添加 android:checked="true",则 mRadioGroup.clearCheck(); 是强制性的,否则不是。
    【解决方案2】:

    我不确定这个问题,但可能是您错过了参考您的广播组 ID。 试试这个来看看效果(在你的onCreate()里面):

    RadioGroup rdGroup = (RadioGroup) findViewById(R.id.footer_radio);
        rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {
                RadioButton btn = (RadioButton)findViewById(checkedId);
                if(btn != null){
                      do_something();
                }
            }
        });
    

    或者可能是(RadioButton)findViewById(checkedId) 返回null。

    如果您的情况不明确,请尝试Spinner

    【讨论】:

      【解决方案3】:

      而不是btn.setChecked(true)

      使用btn.performClick(); 这将起作用。

      【讨论】:

        【解决方案4】:

        我有同样的问题..

        这是我的方法:

         class Myclass extends Activity{
            protected void onCreate(Bundle savedInstanceState) {
        
            radiogroup = (RadioGroup) findViewById(R.id.radiogroupDecisionUncertain);
                    radio1 = (RadioButton) findViewById(R.id.radiobuttonNo_U);
                    radio2 = (RadioButton) findViewById(R.id.radiobuttonYes_U);
        // declare the onCheckChangedListener
        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        
            if (checkedId == radio1.getId()) { //do something
        
                        } else { // do other thing
                        }
        
                    }
                });
        
        radiogroup.check(radio1.getId());
        
        
        }
        }
        

        请注意,我在注册监听器后设置了选中的单选按钮..

        【讨论】:

          【解决方案5】:

          实现 OnCheckedChangeListener 而不是 RadioGroup.onCheckedChangeListener 会起作用。

          public class MainActivity extends ActivityGroup implements OnCheckedChangeListener{
          .....
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-21
            • 2013-08-27
            • 2011-05-01
            • 2017-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-07
            相关资源
            最近更新 更多