onCheckedChanged for radioGroups in different Fragments, fragments are created using ViewPager

onCheckedChanged for radioGroups in different Fragments, fragments are created using ViewPager

我有 6 个片段,每个片段都有一个包含 4 个单选按钮的 RadioGroup,它们都使用 viewpager 显示在同一个 activity 上,因此用户可以滑动浏览它们。

我一直在尝试在每个 RadioGroup 上实现一个 onCheckedChangedListener,这样我就可以知道在每个片段中检查了哪个单选按钮。

过去几天我尝试了很多事情,我的工作解决方案非常冗长,肯定有更好的方法。

这是我的 activity 调用其中包含当前解决方案的片段

public class QuestionsActivity extends AppCompatActivity {

Toolbar toolbar;
ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_questions);

//        tool bar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    /** set the adapter for ViewPager */
    mViewPager.setAdapter(new MyPagerAdapter(
            getSupportFragmentManager()));
}


/**
 * Defining a FragmentPagerAdapter class for controlling the fragments to be shown when user swipes on the screen.
 */
public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        /** Show a Fragment based on the position of the current screen */
        if (position == 0) {
            return new Q1Fragment();
        } else if (position == 1) {
            return new Q2Fragment();
        } else if (position == 2) {
            return new Q3Fragment();
        } else if (position == 3) {
            return new Q4Fragment();
        } else if (position == 4) {
            return new Q5Fragment();
        } else
            return new Q6Fragment();
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 6;
    }

}



public void onRadio1Clicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()) {
        case R.id.radioButton1:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio1";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton2:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio2";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton3:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio3";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton4:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio4";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
    }
}

public void onRadio2Clicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()) {
        case R.id.radioButton1:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio1";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton2:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio2";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton3:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio3";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
        case R.id.radioButton4:
            if (checked) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                CharSequence text = "radio4";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
    }
}
}

对于每个无线电组,我使用 xml 中的 android:onClick 函数并为每个无线电组创建一个新方法。以上只是其中的两个。

这是片段的样子

<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="310dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="18dp"
    android:padding="10dp"
    android:text="@string/radio"
    android:onClick="onRadio1Clicked"
    />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="310dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="18dp"
    android:padding="10dp"
    android:text="@string/radio1"
    android:onClick="onRadio1Clicked"/>

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="310dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="18dp"
    android:padding="10dp"
    android:text="@string/radio2"
    android:onClick="onRadio1Clicked"/>

<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="310dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="18dp"
    android:padding="10dp"
    android:text="@string/radio3"
    android:onClick="onRadio1Clicked"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/submit"
    android:layout_marginTop="13dp"
    />

</RadioGroup>

我已经尝试在主 activity 和片段 activity 中使用类似的侦听器,但我得到了第二个无线电组的空指针异常。

这是监听器的变体,这是我尝试在片段中使用的一个 activity

RadioGroup rg1 = (RadioGroup)     getView().findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()

{

    @Override
    public void onCheckedChanged (RadioGroup group,int checkedId){
    Context context = getContext();
    RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
    Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
    toast.show();
}
}

);

无论我做什么,我都无法让听众处理第一个片段以外的片段。

有什么想法吗?

只需从您的布局中删除所有 android:onClick="onRadio1Clicked" 并从您的 activity 中删除 setOnCheckedChangeListener 代码并在片段中实现它,例如在 Q1Fragment 中覆盖 onViewCreated 方法并在那里编写代码,依此类推在其他片段中。

在 Q1 片段中:-

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RadioGroup rg1 = (RadioGroup)     view.findViewById(R.id.radioGroup1);
        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{

    @Override
    public void onCheckedChanged (RadioGroup group,int checkedId){
    Context context = getContext();
    RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
    Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
    toast.show();
}
}

);
    }

在 Q2Fragment 中:-

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RadioGroup rg2 = (RadioGroup)     view.findViewById(R.id.radioButton2);
        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{

    @Override
    public void onCheckedChanged (RadioGroup group,int checkedId){
    Context context = getContext();
    RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
    Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
    toast.show();
}
}

);
    }