【发布时间】:2017-02-04 01:30:32
【问题描述】:
我是这个 Android 世界的新手,我正在学习制作应用程序,所以我遇到了问题。
工作中
我创建了一个 fragment,在里面我有一个 radio 组,其中包含 3 个单选按钮
目标
当用户返回此屏幕时,清除片段内的所有单选按钮
问题
我不知道如何做到这一点
问题
如何清除单选按钮的所有勾选?
已完成的步骤
我尝试了以下方法:
但我好像做不到
代码
这段代码对我不起作用(来自上面的帖子)
protected void onResume()
{
RadioGroup rg=(RadioGroup)findViewById(R.id.RG);
rg.clearCheck();
super.onResume();
}
但我有以下几点:
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);
//This piece is for testing purposes
openSelect.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (surfArea.isChecked())
{
Intent sa = new Intent(getContext(), OperSphere.class);
startActivity(sa);
}
}
});
return rootView;
}
//Here is where I'm stuck
@Override
public void onResume()
{
super.onResume();
}
}
我有兴趣将代码放入onResume()
我知道有关于片段 (https://developer.android.com/guide/components/fragments.html) 的文档,但这些文档无法回答我的问题
提前致谢
【问题讨论】:
-
对我来说,首先调用 super.onResume() 让 Android 框架做任何需要恢复然后运行代码的操作似乎更合乎逻辑。
-
你能详细说明吗?
-
super.onResume(); RadioGroup rg=(RadioGroup)findViewById(R.id.RG); rg.clearCheck();
-
不行,Android Studio 报错“无法解析方法
findViewById” -
然后调用 radG =(RadioGroup)findViewById(R.id.RG);在您的 onCreateView 方法中。在 super.onResume() 之后只需调用 radG.clearCheck();
标签: android android-fragments radio-button radio-group