【问题标题】:how to pass a radiogroup value from a fragment to an activity?如何将radiogroup值从片段传递给活动?
【发布时间】:2018-08-19 10:35:50
【问题描述】:

我如何从片段中的无线电组中获取值, 我有 1 个片段来显示问题,我想获得价值 用户对活动的回答,因为下一步和后退按钮在活动中, 我来自 sqlite 数据库的问题

我的活动片段

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



    viewPager = findViewById(R.id.view_pager);
    setupViewPager(viewPager);

    back = findViewById(R.id.btnBack);
    next = findViewById(R.id.btnNext);
    back.setOnClickListener(this);
    next.setOnClickListener(this);
}

显示从frament到Activity使用ViewPager的问题

 private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(0));, "1");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(1));, "2");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(2);, "3");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(3));, "4");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(4));, "5");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(5);, "6");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(6));, "7");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(7));, "8");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(8));, "9");
        adapter.addFrag(QuestionFragment.newInstance(String.valueOf(9));, "10");
        viewPager.setOffscreenPageLimit(adapter.getCount());
        viewPager.setAdapter(adapter);
    }

按钮下一步和返回

@Override
public void onClick(View view) {
    if(view == back){
        mfragmentIndex--;
        viewPager.setCurrentItem(mfragmentIndex);


    }else if(view == next) {
            mfragmentIndex++;
            viewPager.setCurrentItem(mfragmentIndex);

    }
}

类视图寻呼机

class ViewPagerAdapter extends FragmentPagerAdapter{
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitle = new ArrayList<>();

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

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    public void addFrag(Fragment fragment,String title){
        mFragmentList.add(fragment);
        mFragmentTitle.add(title);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
}

我的片段展示问题

public static QuestionFragment newInstance(String questionId1) {
    final QuestionFragment fragment = new QuestionFragment();
    final Bundle args = new Bundle();
    args.putString(ARG_QUESTION_ID,questionId1);
    fragment.setArguments(args);
    return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final String question1 = getArguments().getString(ARG_QUESTION_ID);
    final DBAdapter dbAdapter = new DBAdapter(getContext());
    questionsList = dbAdapter.getAllQuestions();
    currentQuestion = questionsList.get(Integer.parseInt(question1));
    View view = inflater.inflate(R.layout.fragment_question, container, false);
    final RadioGroup grp=view.findViewById(R.id.radioGroup1);

    txtQuestion=view.findViewById(R.id.tvQuestion);
    rbtnA=view.findViewById(R.id.radio0);
    rbtnB=view.findViewById(R.id.radio1);
    rbtnC=view.findViewById(R.id.radio2);
    rbtnD=view.findViewById(R.id.radio3);
    setPertanyaan();
    return view;
}
private void setPertanyaan()
{

    txtQuestion.setText(currentQuestion.getQUESTION());
    rbtnA.setText(currentQuestion.getOptionA());
    rbtnB.setText(currentQuestion.getOptionB());
    rbtnC.setText(currentQuestion.getOptionC());
    rbtnD.setText(currentQuestion.getOptionD());
}

【问题讨论】:

    标签: android android-fragments android-radiogroup


    【解决方案1】:

    可以在fragment类中创建一个接口,然后在activity中实现。

    在片段中创建一个接口,如:-

    // Container Activity must implement this interface
    public interface OnRadioGroupSelectedListener {
        public void onButtonSelected(String value);
    }
    

    在片段类中全局声明listner对象:-

    OnRadioGroupSelectedListener mCallback;
    

    Fragment 在其 onAttach() 生命周期方法期间捕获接口实现,然后可以调用接口方法以与 Activity 通信。

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnRadioGroupSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnRadioGroupSelectedListener");
        }
    }
    

    现在接收回调添加:-

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
           // if(checkedId==R.id.radio0)  {
          //      mCallback.onButtonSelected(0)
           //  }else if (checkedId==R.id.radio1)  {
           //     mCallback.onButtonSelected(1);
    
           //  }else if (checkedId==R.id.radio2)  {
            //      mCallback.onButtonSelected(2);
    
           //  }else if (checkedId==R.id.radio3)  {
           //     mCallback.onButtonSelected(3);
    
           //  }
         RadioButton radioButton = group.findViewById(checkedId);
    
         mCallback.onButtonSelected(radioButton.getText());
    
    
    });
    

    回调将选中的单选按钮的位置传递给活动。

    现在在活动中实现lister,

    implements QuestionFragment.OnRadioGroupSelectedListener
    

    并重写该方法以获取活动中所选按钮的位置。

    注意:- 您也可以根据需要更改 onButtonSelected(String selectedRadioButtonValue) 参数。

    【讨论】:

    • 如何在活动中获得价值单选按钮? public void onButtonSelected(int mfragmentIndex) { if(currentQuestion.getANSWER().equals(mCallback)){ Log.e("cmets","Right Answer"); }else{ Log.e("cmets", "Wrong Answer"); } }
    • 根据您的要求做了一些修改,希望有所帮助。
    • 非常感谢我爱你
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 2021-07-06
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多