【问题标题】:onCheckedChanged for radioGroups in different Fragments, fragments are created using ViewPageronCheckedChanged 用于不同 Fragment 中的 radioGroups,fragment 是使用 ViewPager 创建的
【发布时间】:2016-09-05 06:52:41
【问题描述】:

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

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

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

这是我的活动,它调用包含当前解决方案的片段

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 函数并为每个无线电组创建一个新方法。以上只是其中的 2 个。

这是片段的样子

<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>

我尝试在主要活动和片段活动中使用监听器,但我得到第二个无线电组的空指针异常。

这是侦听器的变体,这是我尝试在片段活动中使用的一种

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-fragments android-viewpager android-radiogroup


    【解决方案1】:

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

    在 Q1Fragment 中:-

    @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();
    }
    }
    
    );
        }
    

    【讨论】:

    • 谢谢!我没有尝试 onViewCreated() 仅 onActivityCreated()。
    • 我会给你一些代表,但我只是普通人,还不能这样做。
    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多