页面滑动后片段锁定?

时间:2014-02-02 17:40:01

标签: android

按钮点击是否可以锁定片段? 例如..我正在制作一个测验应用程序但它是片段形式.. 单击提交按钮后。你必须刷到下一页或QUESTION 2.java。 现在的事情是...... 我想禁用PREVIOUS片段,这样你就不能回到问题1了。 感谢你们! 我的代码..

问题1.JAVA

公共类问题1扩展了片段{

RadioButton q1a2;
Button btn1;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.question1, null);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
    btn1 = (Button)getView().findViewById(R.id.btnq1);

    final SharedPreferences app_preferences =  PreferenceManager.getDefaultSharedPreferences(getActivity());
    btn1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            SharedPreferences.Editor editor = app_preferences.edit();
            Toast.makeText(getActivity(), "Submitted",  Toast.LENGTH_SHORT).show();

            if (q1a2.isChecked()){
                editor.putInt("answer_value", 1);
            } else {
                editor.putInt("answer_value", 0);
            }
            editor.commit();
        }
    });

}

}

问题2.JAVA

public class Question2 extends Fragment{

RadioButton q2a1;
Button btn2;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.question2, null);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    q2a1 = (RadioButton)getView().findViewById(R.id.q2a1);
    btn2 = (Button)getView().findViewById(R.id.q2_button);

    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    btn2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            SharedPreferences.Editor editor = app_preferences.edit();

            if (q2a1.isChecked()){
                editor.putInt("answer_value2", 1);
            } else {
                editor.putInt("answer_value2", 0);
            }
            editor.commit();
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

当用户向前移动时,您可以动态地从viewpager中删除旧片段:

dynamically add and remove view to viewpager