简体   繁体   English

我怎样才能每次更改片段中的文本? 对于每个问题都需要相同片段的测验应用程序

[英]How can i change the texts in my fragments each time? For a quiz app that needs the same fragment for each question

So I am making a quiz app and for each question of course, the layout will look the same but questions and possible answers will be different.所以我正在制作一个测验应用程序,当然对于每个问题,布局看起来都是一样的,但问题和可能的答案会有所不同。 I have seen that using fragments in this case would be suitable and i tried learning it but its confusing.我已经看到在这种情况下使用片段是合适的,我尝试学习它但它令人困惑。 Can someone help me with the syntax etc how i can change the text of the questions, buttons when given a questions and a list of answers?有人可以在语法等方面帮助我吗?在给出问题和答案列表时,我如何更改问题的文本、按钮?

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;

public class QuestionFragment extends Fragment {

    Button btnOption1, btnOption2, btnOption3, btnOption4;
    Button btnSubmit;
    TextView txtQuestion;

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

        View questionFragmentView = inflater.inflate(R.layout.question_fragment, container, false);

        btnOption1 = questionFragmentView.findViewById(R.id.btnOption1);
        btnOption2 = questionFragmentView.findViewById(R.id.btnOption2);
        btnOption3 = questionFragmentView.findViewById(R.id.btnOption3);
        btnOption4 = questionFragmentView.findViewById(R.id.btnOption4);
        btnSubmit = questionFragmentView.findViewById(R.id.btnSubmit);
        txtQuestion = questionFragmentView.findViewById(R.id.txtQuestion);

        return questionFragmentView;
    }
}

this is the Fragment which i have created.这是我创建的片段。

片段看起来像这样:

How can I change the Question and button texts each time for a new question?如何每次为新问题更改问题和按钮文本?

Thank you very much!非常感谢你!

I suggest you to use view pager along with fragment for question answer and manage accordingly your requirement.我建议您使用 view pager 和 fragment 来回答问题并相应地管理您的要求。

public class QuestionPagerActivity extends ActionBarActivity
    implements ViewPager.OnPageChangeListener {

ViewPager mViewPager;
ArrayList<Question> mQuestions; // create custom model with all quetions and oprions data

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    setContentView(R.layout.question_pager_activity);

    FragmentManager fm = getSupportFragmentManager();

    mViewPager = (ViewPager) findViewById(R.id.viewPagerQuestion);
    mViewPager.setOnPageChangeListener(this);
    mViewPager.setAdapter(new QuestionAdapter(fm));
    onPageSelected(0);

    mQuestions = Quiz.get(getBaseContext()).getQuestions();

    int index = getIntent().getIntExtra(QuestionFragment.EXTRA_QUESTION_INDEX, 0);
    Question q = mQuestions.get(index);
    for (int i = 0; i < mQuestions.size(); i++) {
        if (mQuestions.get(i).getId() == q.getId()) {
            mViewPager.setCurrentItem(i);
        }
    }
}

@Override
protected void onPause() {
    super.onPause();
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

@Override
protected void onResume() {
    super.onResume();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

private class QuestionAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Question> mQuestions;

    private QuestionAdapter(FragmentManager fm) {
        super(fm);
        mQuestions = Quiz.get(getBaseContext()).getQuestions();
    }

    @Override
    public Fragment getItem(int i) {
        return  QuestionFragment.newInstance(i);
    }

    @Override
    public int getCount() {
        return mQuestions.size();
    }
}

@Override
public void onPageScrolled(int i, float v, int i2) { }

@Override
public void onPageSelected(int i) {
    setTitle(String.format("Question #%s", i + 1));
}

@Override
public void onPageScrollStateChanged(int state) {

    if (state == ViewPager.SCROLL_STATE_IDLE) {
        // Hide the soft keyboard.
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);

    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM