【问题标题】:radiobuttons and spinners in shared preferences?共享首选项中的单选按钮和微调器?
【发布时间】:2014-05-14 12:31:22
【问题描述】:

我在 RadioGroup 中有两个性别单选按钮

1)男性

2)女性

我想将它存储在 sharedpreferences 中,并在我再次打开它时检索它。

除了我想将微调器保存在 sharedpreferences 中,我怎样才能保存它。

【问题讨论】:

    标签: android


    【解决方案1】:
    // Try this way,hope this will help you...
    
    activity_main.xml
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
       <RadioGroup
           android:id="@+id/rdgGender"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
    
           <RadioButton
               android:id="@+id/rdbMale"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Male"/>
    
           <RadioButton
               android:id="@+id/rdbFemale"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Female"/>
           </RadioGroup>
    </LinearLayout>
    
    MainActivity.java
    
    public class MainActivity extends Activity{
    
        private RadioGroup rdgGender;
        private RadioButton rdbMale;
        private RadioButton rdbFemale;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rdgGender = (RadioGroup) findViewById(R.id.rdgGender);
            rdbMale = (RadioButton) findViewById(R.id.rdbMale);
            rdbFemale = (RadioButton) findViewById(R.id.rdbFemale);
    
            rdgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    switch (checkedId){
                        case R.id.rdbMale:
                            editor.putInt("genderValue", R.id.rdbMale);
                            break;
                        case R.id.rdbFemale:
                            editor.putInt("genderValue",R.id.rdbFemale);
                            break;
                    }
                    editor.commit();
                }
            });
    
    
            SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
            switch (sharedPreferences.getInt("genderValue",R.id.rdbMale)){
                case R.id.rdbMale:
                    rdbMale.setChecked(true);
                    break;
                case R.id.rdbFemale:
                    rdbFemale.setChecked(true);
                    break;
            }
    
        }
    }
    

    【讨论】:

    • 我这里有点问题。当我设置女性。重新运行应用程序后。收音机默认为男性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多