简体   繁体   中英

Shared preferences user name

I have an app that I'd like to be able to let a user customize as far as setting their user name, email address and picture to the navDrawer from the preference menu. I'm not 100% sure how to do this though. I'm not sure how to set up the listeners to get the key from the preference screen. Also since these values have id's can they be replaced by the user from the preference they put in?

Any help would be great.

Since you want these values to persist through each use of the app, use SharedPreferences to save the values. Then retrieve and set them in your navDrawer adapter

 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
    pref.edit().putString("name", nameEditText.getText().toString()).commit(); 

To retrieve...

pref.getString("name", defaultValue);

Any additional "putString" using the same key will overwrite the data.

Hopefully this answers your question. If not I can help further. The documentation will help you too.

EDIT Refer to comments below.

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.drawable.ic_playlist, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerStateChanged(int newState) {
            super.onDrawerStateChanged(newState);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM