2016-03-31 4 views
0

Я хочу проверить, проверен ли переключатель на радио. Но внутри фрагмента это не работает, я не могу понять из-за чего? Если кто-то может помочь оценить его очень!Radio Button setOnChangeListener не работает внутри фрагмента

Мой фрагмент:

public class OneFragment extends Fragment { 

RadioGroup radioFanStatus; 
RadioButton radioButtonOn; 
RadioButton radioButtonOff; 
View inflateView; 
TabDisplay tabDisplay; 
public OneFragment() 
{ 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    inflateView=inflater.inflate(R.layout.fragment_one, container, false); 

    radioFanStatus = (RadioGroup)inflateView.findViewById(R.id.radioOnOff); 
    radioButtonOn=(RadioButton)inflateView.findViewById(R.id.radioFanOn); 
    radioButtonOff=(RadioButton)inflateView.findViewById(R.id.radioFanOff); 


     radioFanStatus.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      Toast.makeText(getContext(),"On",Toast.LENGTH_SHORT).show(); 

     // Log.e("",""+checkedId); 
      Toast.makeText(getContext(),"On"+checkedId+"",Toast.LENGTH_SHORT).show(); 

      // checkedId is the RadioButton selected 
      if(checkedId ==R.id.radioFanOn) 
      { 
       Toast.makeText(getContext(),"On"+checkedId+"",Toast.LENGTH_SHORT).show(); 
       ((TabDisplay)getActivity()).functionFanOn(); 
       Log.e("","on"); 
      } 
      else if(checkedId ==R.id.radioFanOff) 
      { 
       Toast.makeText(getContext(),"Off"+checkedId+"",Toast.LENGTH_SHORT).show(); 
       ((TabDisplay)getActivity()).functionFanOff(); 
       Log.e("","off"); 
      } 
      else 
      { 

      } 
     } 
    }); 
    return inflater.inflate(R.layout.fragment_one, container, false); 
} 

} 

Это мой XML связан с фрагментом:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Change staus" 
    android:textSize="40dp" 
    android:layout_centerInParent="true"/> 

<RadioGroup 
    android:id="@+id/radioOnOff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/radioFanOn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="On" 
     /> 

    <RadioButton 
     android:id="@+id/radioFanOff" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Off" /> 


</RadioGroup> 

+0

Что не работает точно? У вас есть ошибки? Что вы пытались исправить? Пожалуйста, прочитайте и следуйте инструкциям на этой странице, чтобы получить качественный вопрос: http://stackoverflow.com/help/how-to-ask –

ответ

1

Попробуйте изменить

return inflater.inflate(R.layout.fragment_one, container, false); 

в

return inflateView; 

Теперь вы раздуваете макет дважды.

+0

да !!! это сработало !! Большое вам спасибо, я потратил много времени, чтобы сделать это. Еще раз спасибо. –

+0

Добро пожаловать. Пожалуйста, примите ответ, если это было полезно –

Смежные вопросы