我在 fragment 中有 3 个按钮,我想在主要 Activity 中禁用它们。我曾尝试在 replace() newfragment 之前使用 bundle 选项,但它产生了其他问题。 fragment 中的按钮可以转换为主要 Activity 吗?像这样的东西。我收到无法转换的错误,但我的条目不正确。
RedUp = (ButtonControls) getFragmentManager().findFragmentById(R.id.btnRedUP);
这是我的 main.xml,它包含名为 rgb_controls 的 fragment
<FrameLayout
android:id="@+id/rgb_controls"
android:layout_width="390dp"
android:layout_height="match_parent"
android:layout_marginLeft="550dp"
android:layout_marginTop="10dp" >
</FrameLayout>
这是名为 button_controls.xml 的 fragment xml 中的按钮
<Button
android:id="@+id/btnRedUP"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="80dp"
android:text="UP"
android:textSize="30dp" />
这是 fragment Activity
public class ButtonControls extends Fragment {
public Button RedUp, RedDn, GreenUp, GreenDn, BlueUp, BlueDn;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.button_controls, container, false);
RedUp = (Button) view.findViewById(R.id.btnRedUP);
return view;
}
}
使用 replace() 添加 fragment
Fragment ButtonFragment = new ButtonControls();
ButtonFragment.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.rgb_controls, ButtonFragment).addToBackStack(null).commit();
最佳答案
像这样的事情应该做的事情:
ButtonControls fragment = (ButtonControls)
getFragmentManager().findFragmentById(R.id.rgb_controls);
fragment.setButtonsGone();
您必须在 ButtonControls
中实际实现此 setButtonsGone
方法。东西如:
public void setButtonsGone() {
RedUp.setVisibility(View.GONE);
RedDn.setVisibility(View.GONE);
GreenUp.setVisibility(View.GONE);
GreenDn.setVisibility(View.GONE);
BlueUp.setVisibility(View.GONE);
BlueDn.setVisibility(View.GONE);
}
关于android - 从主要 Activity 中禁用 fragment 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124557/