【问题标题】:Android - How to uncheck radio buttons in a radio group inside a fragment?Android - 如何取消选中片段内单选组中的单选按钮?
【发布时间】: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


【解决方案1】:

1) 初始化您在 onCreateView 方法中的 RadioGroup 为

private RadioGroup rg;

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);

    // initialize your radiogroup here
    rg = (RadioGroup) rootView.findViewById(R.id.RG);

    .....
    // Rest of your code
}

2) 现在调用以下方法,即,clearSelection() 无论您想取消选中 RadioButtons 的任何位置(但在上述代码之后)。

private void clearSelection(){
    if(rg != null) rg.clearCheck();  
}

3) 示例:如果你想取消选中 onResume() 中的 RadioButtons,你可以从那里调用它

@Override
public void onResume(){
    super.onResume();

    clearSelection();
}

如果你想通过其他方法做到这一点,它甚至可以工作

private void myMethod(){

    // need to clear radio buttons
    clearSelection();

    // rest of my code
}

【讨论】:

    【解决方案2】:

    您是否尝试过在您想要实现的目标之前拨打电话super.onResume()...

    【讨论】:

    • 是的,但 Android Studio 抱怨这一点,它标志着“无法解析方法findViewById
    • 你能解释一下你到底在做什么......在哪里找不到'findViewById'??
    • Android Studio 告诉我它无法在 public void onResume() 内解析此方法 findViewById,当我将 public 更改为 protected 时,它说“(我的包)中的 onResume()onResume in android.support.v4.app.Fragment 试图分配较弱的访问权限
    • 好的..首先你不能分配较弱的访问权限'从公共到受保护'不要这样做......也试试 RadioGroup radG;= (RadioGroup) rootView.findViewById(R.id.RG);在你 onCreateView 然后在你的简历中只使用 radG.clearCheck();
    【解决方案3】:

    在 onResume 回调中调用 radioGroup 的 clearCheck() 方法,类似于:

    @Override
    protected void onResume() {  
    super.onResume();  
    rg.clearCheck();
        } 
    

    【讨论】:

      【解决方案4】:

      对于面临相同问题的任何人在片段之间导航时可以这样做以使其正常工作。

      radioButton.setSaveEnabled(false);
      

      确保对组中的所有单选按钮而不是 radioGroup 本身这样做

      注意:此标志只能禁用此视图的保存;任何子视图可能仍保存其状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-10
        • 2023-03-29
        • 2011-10-14
        • 2012-04-30
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多