[英]Android - How to enable a button if a radio button is checked?
I want to know/learn how to enable a button when/if a radio button is checked 我想知道/了解如何/当单选按钮被选中时启用按钮
Steps done 完成步骤
I created a fragment and inside a radio group with 3 radio buttons inside 我创建了一个片段,并在一个带有3个单选按钮的单选组中
Goal 目标
My main goal is to enable the button when a radio button is checked, and disable it when the radio button is unchecked 我的主要目标是在选中单选按钮时启用该按钮,而在取消选中该单选按钮时将其禁用
Code 码
So far I have this code 到目前为止,我有这段代码
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();
}
}
Question 题
How to enable a button when a radio button is checked? 选中单选按钮时如何启用按钮?
Some examples would be great 一些例子会很棒
Thanks in advance 提前致谢
Sorry my bad, here try this solution; 不好意思,请尝试以下解决方案;
Since you are using RadioGroup, set the listener on this to disable or enable button accordingly. 由于您正在使用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;
}
}
});
Just use following snippet: 只需使用以下代码段:
@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;
}
}
You could set a listener on the radio button like others have suggested, then toggle the radio button on and off using the function you made. 您可以像其他建议的那样在单选按钮上设置一个侦听器,然后使用您创建的功能打开和关闭单选按钮。
radG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButtonId) {
toggleCheck();
}
}
});
Where toggleCheck()
can be something like this: 其中
toggleCheck()
可能是这样的:
if (radG.isChecked()) {
radG.clearCheck();
button.setEnabled(false);
} else {
radG.select();
button.setEnabled(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.