Android - 如果单选按钮被选中,如何启用按钮?
Android - How to enable a button if a radio button is checked?
我想know/learn如何启用按钮when/if选中单选按钮
完成的步骤
我创建了一个 片段 并在一个单选组中,其中有 3 个单选按钮
目标
我的主要目标是在选中单选按钮时启用该按钮,并在取消选中该单选按钮时禁用它
代码
到目前为止我有这个代码
public class Operations extends Fragment
{
RadioButton surfArea, rad, diam;
RadioGroup radG;
Button openSelect;
public Operations()
{
// Required empty public constructor
}
public static Operations newInstance()
{
return new Operations();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);
surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
openSelect = (Button) rootView.findViewById(R.id.btn_open_select);
radG = (RadioGroup) rootView.findViewById(R.id.RG_group);
openSelect.setEnabled(false);
openSelect.setOnClickListener(new View.OnClickListener()
{ //This piece of code is for testing purposes
@Override
public void onClick(View v)
{
if (surfArea.isChecked())
{
openSelect.setEnabled(true); //I detected my mistake here, so I would like to know a better way to achieve this
Intent sa = new Intent(getContext(), OperSphere.class);
startActivity(sa);
}
}
});
return rootView;
}
@Override
public void onResume()
{ //Here the radio button unchecks
radG.clearCheck();
super.onResume();
}
}
问题
选中单选按钮时如何启用按钮?
一些例子会很棒
提前致谢
对不起我的错,在这里试试这个解决方案;
由于您使用的是 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.surfArea:
//enable or disable button
break;
case R.id.rad:
//enable or disable button
break;
case R.id.diam:
//enable or disable button
break;
}
}
});
只需使用以下代码段:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.RB_surfArea:
// here set button enable/disable to true / false
break;
case R.id.RB_Rad:
// here set button enable/disable to true / false
break;
case R.id.RB_Diam:
// here set button enable/disable to true / false
break;
}
}
您可以像其他人建议的那样在单选按钮上设置一个侦听器,然后使用您创建的功能打开和关闭单选按钮。
radG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButtonId) {
toggleCheck();
}
}
});
其中 toggleCheck()
可以是这样的:
if (radG.isChecked()) {
radG.clearCheck();
button.setEnabled(false);
} else {
radG.select();
button.setEnabled(true);
}
我想know/learn如何启用按钮when/if选中单选按钮
完成的步骤
我创建了一个 片段 并在一个单选组中,其中有 3 个单选按钮
目标
我的主要目标是在选中单选按钮时启用该按钮,并在取消选中该单选按钮时禁用它
代码
到目前为止我有这个代码
public class Operations extends Fragment
{
RadioButton surfArea, rad, diam;
RadioGroup radG;
Button openSelect;
public Operations()
{
// Required empty public constructor
}
public static Operations newInstance()
{
return new Operations();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);
surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
openSelect = (Button) rootView.findViewById(R.id.btn_open_select);
radG = (RadioGroup) rootView.findViewById(R.id.RG_group);
openSelect.setEnabled(false);
openSelect.setOnClickListener(new View.OnClickListener()
{ //This piece of code is for testing purposes
@Override
public void onClick(View v)
{
if (surfArea.isChecked())
{
openSelect.setEnabled(true); //I detected my mistake here, so I would like to know a better way to achieve this
Intent sa = new Intent(getContext(), OperSphere.class);
startActivity(sa);
}
}
});
return rootView;
}
@Override
public void onResume()
{ //Here the radio button unchecks
radG.clearCheck();
super.onResume();
}
}
问题
选中单选按钮时如何启用按钮?
一些例子会很棒
提前致谢
对不起我的错,在这里试试这个解决方案;
由于您使用的是 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.surfArea:
//enable or disable button
break;
case R.id.rad:
//enable or disable button
break;
case R.id.diam:
//enable or disable button
break;
}
}
});
只需使用以下代码段:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.RB_surfArea:
// here set button enable/disable to true / false
break;
case R.id.RB_Rad:
// here set button enable/disable to true / false
break;
case R.id.RB_Diam:
// here set button enable/disable to true / false
break;
}
}
您可以像其他人建议的那样在单选按钮上设置一个侦听器,然后使用您创建的功能打开和关闭单选按钮。
radG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButtonId) {
toggleCheck();
}
}
});
其中 toggleCheck()
可以是这样的:
if (radG.isChecked()) {
radG.clearCheck();
button.setEnabled(false);
} else {
radG.select();
button.setEnabled(true);
}