如何在 Android 中以编程方式设置单选按钮的(选中)状态?
How to set the (checked) state of a radiobutton programmatically in Android?
我有一个带阀门的无线设备,它通过蓝牙 LE 连接到 Android 应用程序。在应用程序中,我有一个 activity 布局,在 radioGroup 中有 2 个 radioButtons。启动时,我需要查询外部设备以确定阀门的当前状态,并相应地在 App 中设置一个单选按钮。我的基本策略如下...
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) 触发时,它永远不会依次触发 OnCheckedChangeListener,也不会更新 UI 中的 Widget。如果我在 onCreate() 中设置单选按钮的状态,我就可以设置它。我想我的实际问题是......如何在 onCreate() 之外的例程中设置 radioButton?
valveOnRadioButton.setChecked(真);
感谢 Muthukrishnan Rajendran 的回答...
private void refeshUi() {
runOnUiThread(new Runnable() {
public void run() {
if (valveOn) {
valveOnRadioButton.setChecked(true);
}
else {
valveOffRadioButton.setChecked(true);
}
}
});
}
回答晚了,但这对我有用:
((RadioButton)radioGroup.getChildAt(0)).setChecked(true);
此致
我有一个带阀门的无线设备,它通过蓝牙 LE 连接到 Android 应用程序。在应用程序中,我有一个 activity 布局,在 radioGroup 中有 2 个 radioButtons。启动时,我需要查询外部设备以确定阀门的当前状态,并相应地在 App 中设置一个单选按钮。我的基本策略如下...
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) 触发时,它永远不会依次触发 OnCheckedChangeListener,也不会更新 UI 中的 Widget。如果我在 onCreate() 中设置单选按钮的状态,我就可以设置它。我想我的实际问题是......如何在 onCreate() 之外的例程中设置 radioButton?
valveOnRadioButton.setChecked(真);
感谢 Muthukrishnan Rajendran 的回答...
private void refeshUi() {
runOnUiThread(new Runnable() {
public void run() {
if (valveOn) {
valveOnRadioButton.setChecked(true);
}
else {
valveOffRadioButton.setChecked(true);
}
}
});
}
回答晚了,但这对我有用:
((RadioButton)radioGroup.getChildAt(0)).setChecked(true);
此致