gpt4 book ai didi

android - 单选按钮 setOnChange 监听器在 fragment 内不起作用

转载 作者:行者123 更新时间:2023-11-30 01:26:43 24 4
gpt4 key购买 nike

我想检查单选按钮是否被选中然后执行。但是在 fragment 内部这是行不通的,我不知道为什么?如有大神帮忙,感激不尽!

我的 fragment :

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);
}

}

这是我的与 fragment 相关的 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>

最佳答案

努力改变

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

return inflateView;

现在您将布局充气两次。

关于android - 单选按钮 setOnChange 监听器在 fragment 内不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com