![](/img/trans.png)
[英]Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first
[英]Need to disable buttons depending on if a radio button is selected
我正在嘗試在程序員模式下對功能齊全的Windows 7計算器進行編程。 現在,我正在努力使按鈕正常工作。 因此,我希望禁用用於六邊形的按鈕,直到選擇了十六進制單選按鈕。 因此,在dec或Bint模式下,AF按鈕將被禁用,直到另行選擇為止。
這是按鈕A:
JButton button_A = new JButton("A");
button_A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button_A.setEnabled(false);
hexDisable();
textField.setText(textField.getText() + "A");
}
});
這是單選按鈕:
JRadioButton rButton_Hex = new JRadioButton("Hex");
rButton_Hex.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hexDisable();
}
});
rButton_Hex.setBounds(9, 218, 54, 23);
contentPane.add(rButton_Hex);
Group1.add(rButton_Hex);
這是啟用和禁用相應按鈕的方法。
public void hexDisable(){
button_A.setEnabled(true);
}
我對在Java中使用GUI極為陌生。
只是讓setOnCheckedChangeListener您RadioGroup中 :
RadioGroup radG = (RadioGroup) findViewById(R.id.yourRadioGroup);
radG.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId)
{
case R.id.hexa:
//enable or disable button
break;
case R.id.normal:
//enable or disable button
break;
case R.id.other:
//enable or disable button
break;
}
}
});
它只是導入所需的目錄,然后將JRadioButton初始化為“ NULL”以進行打開和關閉,然后將其添加到ButtonGroup!
Import Javax.swing.Jframe;
Private JRadioButton rdbtnOn=null;
Private JRadioButton rdbtnOff=null;
Private ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnOn);
bg.add(rdbtnOFF);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.