SharedPreferences 使用

1,获取到SharedPreferences

2,编辑SharedPreferences SharedPreferences.Editor

3,提交 commit()

4,获取get****

package com.example.listactivity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Sharepreferencesc extends Activity {

    private SharedPreferences _preferences;
    private SharedPreferences.Editor _edit;
    private EditText _username;
    private EditText _password;
    private Button _submit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        initView();
    }
    public void initView() {
        _preferences = this.getSharedPreferences("haoju", MODE_PRIVATE);
        _edit = _preferences.edit();
        
        _username = (EditText)this.findViewById(R.id.username);
        _password = (EditText)this.findViewById(R.id.password);
        _submit   = (Button)this.findViewById(R.id.submit);
        
        _username.setText(_preferences.getString("username", null));
        _password.setText(_preferences.getString("password", null));
        
        _submit.setOnClickListener(new onclick());
        
        
    }
    
    class onclick implements OnClickListener{

        @Override
        public void onClick(View v) {
            _edit.putString("username", _username.getText().toString().trim());
            _edit.putString("password", _password.getText().toString().trim());
            
            _edit.commit();
            
        }
        
    }
}
原文地址:https://www.cnblogs.com/lihaolihao/p/3762456.html