码迷,mamicode.com
首页 > 其他好文 > 详细

SharedPreferences 使用

时间:2014-05-31 18:18:11      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:android   c   style   class   blog   code   

1,获取到SharedPreferences

2,编辑SharedPreferences SharedPreferences.Editor

3,提交 commit()

4,获取get****

 

bubuko.com,布布扣
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();
            
        }
        
    }
}
bubuko.com,布布扣

 

SharedPreferences 使用,布布扣,bubuko.com

SharedPreferences 使用

标签:android   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/lihaolihao/p/3762456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!