问题描述
我在一个片段中有3个按钮,我想从主要活动中禁用它们.我曾尝试在replace()newfragment之前使用bundle选项,但它产生了其他问题.片段中的按钮可以强制转换为主要活动吗?这样的事情.我收到无法投射的错误,但我的输入不正确.
I have 3 buttons in a fragment and I want to disable them from the main activity. I have tried to use the bundle option before replace() newfragment but it created other issues. Can a button in a fragment be casted to a main activity ? Something like this. I get the error cannot cast but my entries are incorrect.
RedUp = (ButtonControls) getFragmentManager().findFragmentById(R.id.btnRedUP);
这是我的main.xml,其中包含名为rgb_controls的片段
Here is my main.xml this holds the fragment called rgb_controls
<FrameLayout
android:id="@+id/rgb_controls"
android:layout_width="390dp"
android:layout_height="match_parent"
android:layout_marginLeft="550dp"
android:layout_marginTop="10dp" >
</FrameLayout>
这是片段xml中名为button_controls.xml的按钮
Here is the button in the fragment xml called button_controls.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" />
这是片段活动
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 is added using the replace()
Fragment ButtonFragment = new ButtonControls();
ButtonFragment.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.rgb_controls, ButtonFragment).addToBackStack(null).commit();
推荐答案
类似这样的事情应该可以解决:
Something like this should do the thing:
ButtonControls fragment = (ButtonControls)
getFragmentManager().findFragmentById(R.id.rgb_controls);
fragment.setButtonsGone();
您必须在 ButtonControls
中实际实现此 setButtonsGone
方法.诸如此类:
You have to actually implement this setButtonsGone
method in ButtonControls
. Something as:
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);
}
这篇关于禁用主要活动片段中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!