菜单中的单选按钮使用SharedPreferences

时间:2016-08-27 04:22:47

标签: android menuitem android-sharedpreferences

尝试将三个单选按钮显示为菜单项,用户只选择一个,即使应用已关闭,也会保存该选项。然后,他们选择的选项将用于确定应用启动时将显示哪个布局片段。

我真的不明白如何在共享首选项中保存用户选择,因为我之前从未使用过它,也不熟悉编码。

我的菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/settings"
    android:title="@string/settings">
    <menu >
        <group android:id="@+id/startScreenGroup" android:checkableBehavior="single">
            <item android:id="@+id/submenu0" android:title="Fragment 0" />
            <item android:id="@+id/submenu1" android:title="Fragment 1" />
            <item android:id="@+id/submenu2" android:title="Fragment 2" />
        </group>
    </menu>
</item>

</menu>

2 个答案:

答案 0 :(得分:1)

请尝试以下代码,但未经过测试,但基本上通常是这样,如果在尝试使用代码时出现任何错误,请与我们联系。

private RadioGroup radioGroup;
private RadioButton radioButton;

//Fragment references
private Fragment0 fragment0;
private Fragment1 fragment1;
private Fragment2 fragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_detail);

        //Create a SharedPreference variable
        SharedPreferences appPreferences = this.getSharedPreferences("YOUR APP PACKAGE NAME", Context.MODE_PRIVATE);


        final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.startScreenGroup);
       radioGroup.setOnChangedListener(new RadioGroup.OnChangedListener() {

          @Override
         void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {

            case R.id.submenu0:
                 appPreferences.edit().putString("LoadFragment","submenu0");
                 break;
            case R.id.submenu1:
                 appPreferences.edit().putString("LoadFragment","submenu1");
                 break;
            case R.id.submenu2:
                appPreferences.edit().putString("LoadFragment","submenu2");
                break;
             }

        appPreferences.commit();        
    }
});


        //get menuID
        String fragmentToLoad = appPreferences.getString("LoadFragment","");

       //load user selected fragment
       if(fragmentToLoad != null){

       switch(fragmentToLoad){
          case "submenu0" : 
          this.getSupportFragmentManager().beginTransaction()
              .replace(R.id.container_main, fragment0)
              .commit();
              break;

         case "submenu1" : 
         this.getSupportFragmentManager().beginTransaction()
             .replace(R.id.container_main, fragment1)
             .commit();
             break;

         case "submenu2" : 
         this.getSupportFragmentManager().beginTransaction()
             .replace(R.id.container_main, fragment2)
             .commit();
              break;
        }
   }


}

<强>更新

我终于有时间研究这个问题,100%工作并在2部手机(Lollipop和Marshmallow设备)上进行测试。

public class MainActivity extends AppCompatActivity {

    SharedPreferences appPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Create a SharedPreference variable
        appPreferences = this.getSharedPreferences("com.example.ramithrd.radiobuttonssharedpref", Context.MODE_PRIVATE);

        //get menuID
        String fragmentToLoad = appPreferences.getString("LoadFragment","");

        //load user selected fragment
        if(fragmentToLoad != null){

            switch(fragmentToLoad){
                case "submenu0" :
                    this.getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container_main, new Fragment0())
                            .commit();
                    break;

                case "submenu1" :
                    this.getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container_main, new Fragment1())
                            .commit();
                    break;

                case "submenu2" :
                    this.getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container_main, new Fragment2())
                            .commit();
                    break;
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.submenu0) {
            appPreferences.edit().putString("LoadFragment","submenu0").commit();
            this.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container_main, new Fragment0())
                    .commit();

        } else  if (id == R.id.submenu1) {
            appPreferences.edit().putString("LoadFragment","submenu1").commit();
            this.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container_main, new Fragment1())
                    .commit();
        } else if (id == R.id.submenu2){
            appPreferences.edit().putString("LoadFragment","submenu2").commit();
            this.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container_main, new Fragment2())
                    .commit();
        }


        return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:0)

您的广播组将需要在用户点击某个项目时触发的回调。在该回调中,您可以将点击的值保存为SharedPreference

  1. 在您的活动的onCreate()方法中,找到您的RadioGroup并为其指定OnCheckedChangedListener
  2. 在侦听器onCheckChanged()方法中,检查用户点击的项目。
  3. 使用SharedPreferences.Editor将其保存到磁盘。
  4. 您的代码看起来像这样(未编译或测试):

    public void onCreate(final Bundle savedInstanceBundle) {
    
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.startScreenGroup);
        radioGroup.setOnChangedListener(new RadioGroup.OnChangedListener() {
            @Override
            void onCheckedChanged(RadioGroup group, int checkedId) {
                SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit();
                switch (checkedId) {
                    case R.id.submenu0:
                        editor.putString("my_key", "menu0");
                        break;
                    case R.id.submenu1:
                        editor.putString("my_key", "menu1");
                        break;
                    case R.id.submenu2:
                        editor.putString("my_key", "menu2");
                        break;
                }
                editor.commit();        
            }
        });
    
    }