【发布时间】:2016-09-27 04:16:43
【问题描述】:
我想知道/学习如何在/如果选中单选按钮时启用按钮
已完成的步骤
我创建了一个片段并在一个带有 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();
}
}
问题
当单选按钮被选中时如何启用按钮?
一些例子会很棒
提前致谢
【问题讨论】:
-
单选按钮不能取消选中。您必须手动实现该功能。
-
是的,它在
public void onResume()方法中,我研究了如何做到这一点,并在 SO 中发布了一个问题
标签: android radio-button enable-if