2016-09-26 73 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 
} 
相關問題