通过单击主要活动中的按钮,我需要启用片段按钮

时间:2019-09-14 02:56:41

标签: android android-fragments

我在不同的活动中有两个按钮。通过单击“按钮A​​”,我需要启用“按钮B” MainActivity.java中有“按钮A”,QuestionFragment2018.java中有“按钮B”

        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:id="@+id/clickButton"
        android:layout_centerHorizontal="true"
        android:onClick="buttonClicked"/>

需要启用此片段按钮

    <Button
        android:id="@+id/btnviewexplanation"
        style="@android:style/Widget.Button.Inset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:background="@drawable/testbutton"
        android:enabled="false"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Solution" />

MainActivity.java

    public void buttonClicked (View view){
}

QuestionFragment2018.java

    Button buttonExplanation = (Button) 
   rootView.findViewById(R.id.btnviewexplanation);
    buttonExplanation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            questionWebView.loadUrl("file:///android_asset/" + 
   currentQuestion2018.getExplanationText());

        }
    });

1 个答案:

答案 0 :(得分:0)

最简单的方法

在mainActivity类中,使用.xml中的片段ID为片段创建引用

QuestionFragment2018 QuestionFragment2018=
    (QuestionFragment2018) 
  getSupportFragmentManager().findFragmentById(R.id.yourfragmentig) 

在QuestionFragment2018 doWorke()中创建一个函数,从活动中调用该函数

QuestionFragment2018.doWorke()