2016-09-26 74 views
1

我是Android这个世界的新手,我正在学习制作应用程序,所以我遇到了问题。Android - 如何取消选中片段内的广播组中的单选按钮?

工作我创建了一个片段和内部我有一个包含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),但那些不能回答我的问题

在此先感谢

+0

看起来更符合逻辑的我先调用super.onResume()让Android框架执行任何它所需的操作,然后运行您的代码。 –

+0

你能详细说明吗? – NAYIR55

+0

super.onResume(); RadioGroup rg =(RadioGroup)findViewById(R.id.RG); rg.clearCheck(); –

回答

0

你试图把你的电话super.onResume()你想达到什么之前...

+0

是的,但Android Studio抱怨,它标记“无法解析方法'findViewById'” – NAYIR55

+0

你能解释你到底在做什么..在哪里你找不到'findViewById'? – RohitS

+0

Android Studio告诉我它无法在'public void onResume()'内部解析'findViewById'方法,并且当我将'public'改为'protected'时,它说('我的包中的''onResume()'' onResume'在'android.support.v4.app.Fragment'试图分配较弱的访问权限 – NAYIR55

0

呼叫clearCheck()radioGroup中的方法,在你的onResume回调,是这样的:

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

这段代码失败 – NAYIR55

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()无论你想UNCHECK RadioButtons(但在上面的代码后)。

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

3)例如:如果你想取消选中的onResume()的单选按钮,你可以从那里把它作为

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

    clearSelection(); 
} 

如果您想从其他方法做它,它会即使那样工作

private void myMethod(){ 

    // need to clear radio buttons 
    clearSelection(); 

    // rest of my code 
} 
相关问题