如何在我的片段中使用preference.xml文件的首选项



我的android应用程序中有两个包:第一个包中处理我的NavigationDrawerMainActivity和第二个包中的NavigationDrawer片段。现在,我在第二个包中为一些用户设置创建了一个settings_fragment,并在res/xml中创建了preferences.xml文件。

我的问题:这些片段如何获得这些偏好,以及如何保存它们?在我的settings_fragment.xml中有一个复选框,我希望这个复选框在两个操作之间做出决定。例如

  • 选中复选框:calculate_round()

  • 未选中复选框:calculate()

    我该如何处理?


MainActivity.java:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        FragmentManager fm = getFragmentManager();
        int id = item.getItemId();
        if (id == R.id.notenrechner) {
            // Handle the camera action
            fm.beginTransaction().replace(R.id.content_frame, new NotenrechnerFragment()).commit();
        } else if (id == R.id.begriffe) {
            fm.beginTransaction().replace(R.id.content_frame, new BegriffeFragment()).commit();
        } else if (id == R.id.settings) {
            fm.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
        } else if (id == R.id.teilen) {
        } else if (id == R.id.bewerten) {
        } else if (id == R.id.beenden) {
            fm.beginTransaction().replace(R.id.content_frame, new BeendenFragment()).commit();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

首先,您必须将复选框的值保存在SharedPreferences中。在调用addPreferencesFromResource()、后,将此代码添加到您的settings_fragment

CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("key");
checkBoxPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        boolean checked = ((CheckBoxPreference)preference).isChecked();
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("checkboxValue", checked);
        editor.commit();
        return false;
    }
});

然后你可以得到这个值,并决定检索到的值,

boolean checked = sharedPreferences.getBoolean("checkboxValue", <defaultvalue>);
if(checked) {
    calculate_round();
} else {
    calculate();
}

相关内容

  • 没有找到相关文章

最新更新