android - 从主要 Activity 中禁用 fragment 中的按钮

标签 android android-fragments

我在 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/

相关文章:

Android fragment 事务监听器

android - fragment Activity 在启动时崩溃

android - 从 fragment 中将数据保存在内部存储中?

android - 如何使用 View 寻呼机在 android fragment 选项卡中移动下一页?

java - setOnClickListener 提示我应该使用 setOnItemClickListener

android - YouTube api + fragment

Android获取月份为两位数

java - 错误: threadid=1: thread exiting with uncaught exception (group=0xb60734f0)

android - AVCodecContex 在android中为宽度和高度返回零

android - 决策 : ListView or ScrollView