您的位置:首页 > 移动开发 > Android开发

android:数据的存储与访问----Sharedpreferences

2015-09-02 23:50 597 查看
Sharedpreferences,保存用户偏好数据。

SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据

下面使用Sharedpreferences保存用户登录账户和密码,以及实现在下次登录时将用户名账户和密码显示在文本框中。附带实现自动登录功能。

通过Sharedpreferences存取数据

public class PersonService {
   private Context context;

    public PersonService(Context context) {
        this.context = context;
    }
    /**
     * 
     * 将偏好信息保存在xml文本中
     * @param nameText
     * @param ageText
     */

    public void save(String username, String password ) {
       SharedPreferences sharedPreferences=context.getSharedPreferences("example", Context.MODE_PRIVATE);
       Editor editor = sharedPreferences.edit();
       editor.putString("username", username);
       editor.putString("password", password);
       editor.commit();
    }
    /**
     * 从xml文件中读取偏好信息显示在界面上
     * 
     * @return
     */
    public Map<String, String> getPreferences() {
        // TODO Auto-generated method stub
        Map<String, String> params = new HashMap<String, String>();
        SharedPreferences preferences = context.getSharedPreferences("example", Context.MODE_PRIVATE);
        params.put("username", preferences.getString("username", ""));
        params.put("password", String.valueOf(preferences.getString("password", "")));
        return params;
    }
}


MainActivity .java

public class MainActivity extends Activity {
    EditText  edittext1=null;
    EditText  edittext2=null;
    PersonService personService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        edittext1=(EditText) findViewById(R.id.loginedittext1);
        edittext2=(EditText) findViewById(R.id.loginedittext2);
        personService=new  PersonService(this);
        Map<String, String> params = personService.getPreferences();
        if(params.get("username")!=null
                &&!params.get("username").equals("")
                &¶ms.get("password")!=null
                &&!params.get("password").equals("")){

                edittext1.setText(params.get("username"));
                edittext2.setText(params.get("password"));
                if(Check(params.get("username"),params.get("password"))){
                    Intent intent = new Intent(this, main.class);
                    startActivity(intent);
                }
        }

    @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;
    }

    //验证用户名和密码是否正确
    public void Login(View v){

        String  username=edittext1.getText().toString();
        String  password=edittext2.getText().toString();

        if(Check(username,password)){
            //personService.save(username, password);
            Intent intent = new Intent(this, main.class);
            startActivity(intent);
        }
    }
    private boolean Check(String username, String password) {
        // TODO Auto-generated method stub

        if(username!=null&&password!=null){
            if(username.equals("atm")&&password.equals("0425")){
                return true;
            }
        }
        return false;
    }
}


数据的存储与访问—-文件方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: