RadioGroup中的单选按钮无法正常工作

时间:2015-02-04 09:02:29

标签: android

我有两个RadioGroup的{​​{1}}:

RadioButton

我初始化<RadioGroup android:id="@+id/main_radiogroup_lock" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/main_layout_lock_dummy" android:orientation="horizontal" > <RadioButton android:id="@+id/main_radiobutton_lock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="110dp" android:background="@drawable/custom_radiobutton_lock" android:button="@null" android:contentDescription="@string/imageview_description" /> <RadioButton android:id="@+id/main_radiobutton_unlock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="230dp" android:background="@drawable/custom_radiobutton_unlock" android:button="@null" android:checked="true" android:contentDescription="@string/imageview_description" /> </RadioGroup> 并设置RadioButton

OnCheckedChangeListener

因此,当我现在启动应用程序时,我的... this.radioButtonLock = (RadioButton) view.findViewById(R.id.main_radiobutton_lock); this.radioButtonLock.setOnCheckedChangeListener(this); this.radioButtonUnlock = (RadioButton) view.findViewById(R.id.main_radiobutton_unlock); this.radioButtonUnlock.setOnCheckedChangeListener(this); ... @Override public void onCheckedChanged(CompoundButton view, boolean isChecked) { if(view.getId() == R.id.main_radiobutton_lock) { Log.v("MainFragment", "Lock"); this.imageButtonLanguage.setEnabled(false); this.buttonOpen.setEnabled(false); this.buttonAutomatic.setEnabled(false); this.buttonClose.setEnabled(false); this.buttonFog.setEnabled(false); this.checkBoxRed.setEnabled(false); this.checkBoxGreen.setEnabled(false); this.checkBoxBlue.setEnabled(false); return; } else if(view.getId() == R.id.main_radiobutton_unlock) { Log.v("MainFragment", "Unlock"); this.imageButtonLanguage.setEnabled(true); this.buttonOpen.setEnabled(true); this.buttonAutomatic.setEnabled(true); this.buttonClose.setEnabled(true); this.buttonFog.setEnabled(true); this.checkBoxRed.setEnabled(true); this.checkBoxGreen.setEnabled(true); this.checkBoxBlue.setEnabled(true); return; } } 已被检查。当我知道chec radioButtonUnlock没有任何反应并且没有日志输出但是已设置已检查的图像以便检查工作正常。当我再次检查radioButtonLock后,会出现以下输出:

radioButtonUnlock

因此02-04 09:59:31.355: V/MainFragment(533): Lock 02-04 09:59:31.355: V/MainFragment(533): Unlock &#39}被解雇了。之后每次按RadioButton之一时,所有事件都会被触发。按RadioButton后,会发生以下情况:

radioButtonLock

那是什么意思?我设置了不同的ID,为什么总是02-04 09:59:32.850: V/MainFragment(533): Unlock 02-04 09:59:32.850: V/MainFragment(533): Lock 被解雇?

1 个答案:

答案 0 :(得分:0)

试试这个希望会起作用 -

1)将检查更改侦听器设置为无线电组

RedioGroup rg =(RadioGroup)findviewByid(R.id.main_radiogroup_lock);
rg.setcheckchangedlistener(true);

2)处理检查更改侦听器事件

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    switch(checkedId){

    case R.id.rdb1:
        radio_selected = R.id.rdb1;
        Toast.makeText(getApplicationContext(),"first",Toast.LENGTH_LONG).show();

    case R.id.rdb2:
        radio_selected = R.id.rdb2;
        Toast.makeText(getApplicationContext(),"second",Toast.LENGTH_LONG).show();
    }
    }