【问题标题】:How to save the state of rediobutton throughout the application in android?如何在android中的整个应用程序中保存rediobutton的状态?
【发布时间】:2012-01-19 04:34:29
【问题描述】:

我有两个单选组,每个组都有两个单选按钮。 每个组中的第一个单选按钮的默认值为 true (即选中)。 当用户单击任何单选按钮以及用户从其他活动返回时,在这些单选按钮/单选按钮上所做的选择都消失了...... 如何保存/恢复单选组/单选按钮选择状态? 这是我的java代码..一个无线电组..

        final RadioButton radioOn = ( RadioButton )findViewById(
        R.id.rbOn );
    final RadioButton radioOff = ( RadioButton )findViewById(
        R.id.rbOff );
    radioOn.setChecked( true );
    radioOn.setOnClickListener( auto_lock_on_listener );
    radioOff.setOnClickListener( auto_lock_off_listener );

请帮忙。

【问题讨论】:

  • 取一些变量并存储它的值。
  • 我在答案中编辑了示例链接..
  • 看我编辑的答案,我给一段代码..

标签: android radio-button


【解决方案1】:

尝试使用 SharedPreferences 保存您的单选组状态

     RadioGroup rG1 = (RadioGroup)findViewById(R.id.radioGroup1);
        int rG1_CheckId = rG1.getCheckedRadioButtonId();

SharedPreferences rG1Prefs = getSharedPreferences("rG1Prefs", MODE_WORLD_READABLE);
                SharedPreferences.Editor prefsEditor = rG1Prefs.edit();
                prefsEditor.putInt("rG1_CheckId", rG1_CheckId);
                prefsEditor.commit();

并把这行用于取回选中的单选按钮 id。

SharedPreferences rG1Prefs = this.getSharedPreferences("rG1Prefs", MODE_WORLD_READABLE);
        rG1Prefs.getInt("rG1_CheckId", null);

并通过使用此 ID 检查单选按钮。

【讨论】:

    【解决方案2】:

    使用 Shared Preferences,保存单选按钮的状态,或者您可以使用 Application Variables(在该活动中声明为全局静态的变量,您可以在任何活动)

    但我认为共享首选项很好..

    Android - Shared Preferences

    看这个例子How to save login info to Shared Preferences Android(remember details feature)

    编辑:

    public  class Test extends Activity   {     
    
    private SharedPreferences prefs;
    private String prefName = "MyPref";
    private SharedPreferences.Editor editor; 
    private static final String CHECK_STATE = "checkBox_State";
    private boolean check_state;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
    
        //---getting the SharedPreferences object....
        prefs = getSharedPreferences(prefName, MODE_PRIVATE);    
        editor = prefs.edit();
    
        radioOn.setOnClickListener(new OnClickListener()  {
            public void onClick(View v)  {               
                if (((CheckBox) v).isChecked()) {
                    check_state = true;                                 
                } else {
                    check_state = false;                                    
                }
            }
        });
    
                // storing in shared preferences...
        editor.putBoolean(CHECK_STATE, check_state );             
        editor.commit();
        }
       });
       }
    

    【讨论】:

    • 我对此非常陌生。你能给我一个代码吗?
    • 全部看懂需要时间,请直接给我code。
    • 非常感谢您,先生,我将尝试使用该示例进行操作。
    • 先生,我尝试了很多让它工作,但它不起作用。请直接给我一个单选按钮的代码。
    【解决方案3】:

    使用此代码 -

    mFillingGroup.check(whichFilling);
        mAddMayoCheckbox.setChecked(addMayo);
        mAddTomatoCheckbox.setChecked(addTomato);
    
        /**
         * We also want to record the new state when the user makes changes,
         * so install simple observers that do this
         */
        mFillingGroup.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {
                        // As with the checkbox listeners, rewrite the
                        // entire state file
                        Log.v(TAG, "New radio item selected: " + checkedId);
                        recordNewUIState();
                    }
                });
    
        CompoundButton.OnCheckedChangeListener checkListener
                = new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // Whichever one is altered, we rewrite the entire UI state
                Log.v(TAG, "Checkbox toggled: " + buttonView);
                recordNewUIState();
            }
        };
        mAddMayoCheckbox.setOnCheckedChangeListener(checkListener);
        mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener);
    }
    
    /**
     * Handy helper routine to write the UI data to a file.
     */
    void writeDataToFileLocked(RandomAccessFile file,
            boolean addMayo, boolean addTomato, int whichFilling)
        throws IOException {
            file.setLength(0L);
            file.writeInt(whichFilling);
            file.writeBoolean(addMayo);
            file.writeBoolean(addTomato);
            Log.v(TAG, "NEW STATE: mayo=" + addMayo
                    + " tomato=" + addTomato
                    + " filling=" + whichFilling);
    }
    

    而且,this 可以帮助您做任何您需要的事情。

    【讨论】:

      【解决方案4】:

      您可以存储到 SharedPreference 中

      简单例子

      SharedPreferences settings = getSharedPreferences("on_off", 0);    
      boolean silent = settings.getBoolean("onoff", false);
      ////// to set the value use editor object from the SharedPreferences
      
      Editor editor = settings.edit();
      editor.putBoolean("onoff", true);
      editor.commit(); // to save the value into the SharedPreference
      

      【讨论】:

        【解决方案5】:

        您需要重写 onSaveInstanceState(Bundle savedInstanceState) 并将您要更改的应用程序状态值写入 Bundle 参数,如下所示:

        @Override
        
            public void onSaveInstanceState(Bundle savedInstanceState) {
              // Save UI state changes to the savedInstanceState.
              // This bundle will be passed to onCreate if the process is
              // killed and restarted.
              savedInstanceState.putBoolean("MyBoolean", true);
              savedInstanceState.putDouble("myDouble", 1.9);
              savedInstanceState.putInt("MyInt", 1);
              savedInstanceState.putString("MyString", "Welcome back to Android");
              // etc.
              super.onSaveInstanceState(savedInstanceState);
            }
        

        Bundle 本质上是一种存储 NVP(“名称-值对”)映射的方式,它会被传递给 onCreate 和 onRestoreInstanceState,您可以在其中提取如下值:

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          // Restore UI state from the savedInstanceState.
          // This bundle has also been passed to onCreate.
          boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
          double myDouble = savedInstanceState.getDouble("myDouble");
          int myInt = savedInstanceState.getInt("MyInt");
          String myString = savedInstanceState.getString("MyString");
        }
        

        您通常会使用这种技术来存储应用程序的实例值(选择、未保存的文本等)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-04
          • 2012-01-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多