如何在Android中以编程方式设置RadioButton的(检查)状态



我有一个无线设备,该设备在蓝牙LE上连接到Android应用程序。在应用程序中,我在放射线群中具有2个radiobuttons的活动布局。在启动时,我需要查询外部设备以确定阀门的当前状态,并相应地在应用程序中设置RadioButton。我的基本策略如下...

private RadioGroup valveStateRadioGroup;
private RadioButton valveOnRadioButton;
private RadioButton valveOffRadioButton;
static boolean valveOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // set view and init Bluetooth stuff here
    valveStateRadioGroup = (RadioGroup) findViewById(R.id.valve_State_Radio_Group);
    valveOnRadioButton = (RadioButton) findViewById(R.id.valve_on_radioButton);
    valveOffRadioButton = (RadioButton) findViewById(R.id.valve_off_radioButton);
    valveStateRadioGroup.clearCheck();
    valveStateRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                // do normal radioButton CLICKED stuff here
        }
    });
    queryValveState(); // call coms routine to get initial device status
}
public void queryValveState() {
    // code here that sends out a wireless query to the device
}
@Override
public synchronized void onDataAvailable(BluetoothGattCharacteristic characteristic) {
    //reply received from device, parse packet, determine valve status
    valveOn = false; // based on reply from device
    refeshUi();
}
private void refeshUi() {
    if (valveOn) {
        valveOnRadioButton.setChecked(true);
    }
    else {
        valveOffRadioButton.setChecked(true); // <<<<<<<<<<<< THIS is the problem
}

我遇到的问题是,当valveoffradiobutton.setchecked(true(fires时,它又不会触发Oncheckedchangelistener,也不会更新UI中的窗口小部件。如果我在ongreate((中进行射射光顿,我就可以设置RadioButton的状态。我想我的实际问题是...您如何在ongreate((之外的例行程序中设置一个radiobutton?

感谢Muthukrishnan Rajendran的答案...

private void refeshUi() {
    runOnUiThread(new Runnable() {
        public void run() {
            if (valveOn) {
                valveOnRadioButton.setChecked(true);
            }
            else {
                valveOffRadioButton.setChecked(true);
            }
        }
    });
}

valveonradiobutton.setchecked(true(;

答案很晚,但是这对我来说是

((RadioButton(radiogroup.getChildat(0((。setChecked(true(;

问候

最新更新