保存状态的单选按钮和值

保存状态的单选按钮和值

本文介绍了保存状态的单选按钮和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主要活动,我有单选按钮,在方法公共无效onRadioButtonClicked(查看视图)我用值来选择每个单选按钮关联,所以我有两个变量a和b,在那之后我按一下按钮bton我根据该方法ajouter有,a和b的值到database.Until现在一切都奥基,我决定使用OnResume和将OnPause方法来拯救活动的状态,所以当我返回到活动我看到单选按钮选择,问题就来了,当我点击按钮保存信息,我得到0的,我看到所选值A和B的数据库,而不是价值,而奇怪的是,当我再次点击放入系统单选按钮,之后在保存按钮,我得到了正确的价值观!在这里我的问题是如何使a和b的值,而对所选择的单选按钮再次点击?

 公共类ActivityUn延伸活动{
        公共无效ajouter(视图v){
          db.open();
          db.insertMENAGE(A,B);
          db.close();
          Toast.makeText(getApplicationContext(),DonnéesEnregistrées,Toast.LENGTH_SHORT).show();
              }

   @覆盖
保护无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_un);

         按钮bton =(按钮)findViewById(R.id.ajoutUn);
         bton.setOnClickListener(新View.OnClickListener(){

          @覆盖
             公共无效的onClick(视图v){

                   ajouter(五); }
         });

公共无效onRadioButtonClicked(查看视图){
    //是按钮现在检查?

        布尔查=((单选)查看).isChecked();

            //检查哪个单选按钮被点击
            开关(view.getId()){
                案例R.id.rm_13_1:
                    如果(检查)
                          一个= 1;
                        打破;
                案例R.id.rm_13_2:
                    如果(检查)
                        一个= 2;
                        打破;

                案例R.id.rm_14_1:
                    如果(检查)
                        B = 1;

                    打破;
                案例R.id.rm_14_2:
                    如果(检查)
                        B = 2;
                       打破;
                案例R.id.rm_14_3:
                    如果(检查)
                        B = 3;
                    findViewById
                    打破;
                案例R.id.rm_14_4:
                    如果(检查)
                        B = 4;
                          打破;
  }
}

@覆盖
保护无效的onPause(){
    super.onPause();
    共享preferences prefs3 = getShared preferences(preFS_NAME,
        MODE_PRIVATE);
    共享preferences.Editor编辑器= prefs3.edit();
    editor.putBoolean(questionA,rm_13_1.isChecked());
    editor.putBoolean(questionB,rm_13_2.isChecked());
    editor.putBoolean(questionC,rm_14_1.isChecked());
    editor.putBoolean(questionD,rm_14_2.isChecked());
    editor.putBoolean(questionE,rm_14_3.isChecked());
    editor.commit();
}

@覆盖
保护无效onResume(){
    super.onResume();
    共享preferences prefs3 = getShared preferences(preFS_NAME,MODE_PRIVATE);
    rm_13_1 =(单选)findViewById(R.id.rm_13_1);
    rm_13_2 =(单选)findViewById(R.id.rm_13_2);
    rm_14_1 =(单选)findViewById(R.id.rm_14_1);
    rm_14_2 =(单选)findViewById(R.id.rm_14_2);
    rm_14_3 =(单选)findViewById(R.id.rm_14_2);
    布尔rm_13_1A = FALSE;
    布尔rm_13_2A = FALSE;
    布尔rm_14_1A = FALSE;
    布尔rm_14_2A = FALSE;
    布尔rm_14_3A = FALSE;
    rm_13_1A = prefs3.getBoolean(questionA,假);
    rm_13_2A = prefs3.getBoolean(questionB,假);
    rm_14_1A = prefs3.getBoolean(questionC,假);
    rm_14_2A = prefs3.getBoolean(questionD,假);
    rm_14_3A = prefs3.getBoolean(questionE,假);
    rm_13_1.setChecked(rm_13_1A);
    rm_13_2.setChecked(rm_13_2A);
    rm_14_1.setChecked(rm_14_1A);
    rm_14_2.setChecked(rm_14_2A);
    rm_14_3.setChecked(rm_14_3A);
}
 

解决方案

单选按钮应该只存在于一个单选按钮组。的单选按钮的特性是,只有一个在一组,可在一个时间进行选择。如果用户选择C,则所有其他的单选按钮变成取消(想想选择题只能选择一个答案。)如果你正在寻找有选择多个选项,看看Android的的

in the main activity i have radio buttons , in the method public void onRadioButtonClicked(View view) i associate each radio button selected with a value, so i have two variables a and b, after that i click on the button bton i have according to the method ajouter, a and b values into database.Until now everything is okey, i decide to use OnResume and OnPause methods to save the state of activity, so when i return to the activity i see that the radio buttons are selected, the problem comes when i click on button to save info , i get 0 as value for a and b in database instead of values that i saw selected, and the weird thing is that when i click again on thoses radio buttons and after that on save button, i get the right values ! My question here is how to make values of a and b without clicking again on the selected Radio Buttons ?

    public class ActivityUn extends Activity {
        public void  ajouter(View v) {
          db.open();
          db.insertMENAGE(a,b);
          db.close();
          Toast.makeText(getApplicationContext(), "Données Enregistrées",Toast.LENGTH_SHORT).show();
              }

   @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_un);

         Button bton = (Button)findViewById(R.id.ajoutUn);
         bton.setOnClickListener(new View.OnClickListener() {

          @Override
             public void onClick(View v) {

                   ajouter(v);                                            }
         });

public void onRadioButtonClicked(View view) {
    // Is the button now checked?

        boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rm_13_1:
                    if (checked)
                          a=1;
                        break;
                case R.id.rm_13_2:
                    if (checked)
                        a=2;
                        break;

                case R.id.rm_14_1:
                    if (checked)
                        b=1;

                    break;
                case R.id.rm_14_2:
                    if (checked)
                        b=2;
                       break;
                case R.id.rm_14_3:
                    if (checked)
                        b=3;
                    findViewById
                    break;
                case R.id.rm_14_4:
                    if (checked)
                        b=4;
                          break;
  }
}

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
        MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs3.edit();
    editor.putBoolean("questionA", rm_13_1.isChecked());
    editor.putBoolean("questionB", rm_13_2.isChecked());
    editor.putBoolean("questionC", rm_14_1.isChecked());
    editor.putBoolean("questionD", rm_14_2.isChecked());
    editor.putBoolean("questionE", rm_14_3.isChecked());
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
    rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
    rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
    rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
    rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
    rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);
    Boolean rm_13_1A = false;
    Boolean rm_13_2A = false;
    Boolean rm_14_1A = false;
    Boolean rm_14_2A = false;
    Boolean rm_14_3A = false;
    rm_13_1A = prefs3.getBoolean("questionA", false);
    rm_13_2A = prefs3.getBoolean("questionB", false);
    rm_14_1A = prefs3.getBoolean("questionC", false);
    rm_14_2A = prefs3.getBoolean("questionD", false);
    rm_14_3A = prefs3.getBoolean("questionE", false);
    rm_13_1.setChecked(rm_13_1A);
    rm_13_2.setChecked(rm_13_2A);
    rm_14_1.setChecked(rm_14_1A);
    rm_14_2.setChecked(rm_14_2A);
    rm_14_3.setChecked(rm_14_3A);
}
解决方案

Radio buttons should only exist in a radio button group. The characteristics of radio buttons is that only one in a group can be selected at a time. If a user selects C then all the other radio buttons become deselected (Think of a multiple choice question. You can only select one answer) If you're looking to have multiple options selected, look into android's CheckBox view

这篇关于保存状态的单选按钮和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:22