我想检查是否选中单选按钮然后执行。但内部片段这是行不通的我无法弄清楚,因为为什么?如果有人能帮助欣赏它!
我的片段:
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 :(得分:1)
尝试更改
return inflater.inflate(R.layout.fragment_one, container, false);
到
return inflateView;
现在你要对布局进行两次充气。