首页 > 其他 > 详细

sharedPreferences

时间:2015-09-27 01:10:37      阅读:325      评论:0      收藏:0      [点我收藏+]

效果图,

技术分享

sharePreferences.java

package tjuci.edu.dl;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class sharePreferences extends Activity {
    EditText userName, userCode;
    CheckBox chk;
    SharedPreferences prefer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userName = (EditText) findViewById(R.id.userName);
        userCode = (EditText) findViewById(R.id.userCode);
        chk = (CheckBox) findViewById(R.id.checkBox);
        prefer = getSharedPreferences("userInfo", MODE_PRIVATE);
        // System.out.println("========="+"pref是否为null"+pref==null);

        System.out.println("1111111111111");
        boolean b = prefer == null;
        // System.out.println("prefer是否为null"+b);
        String xm = prefer.getString("xingming", "");
        String mm = prefer.getString("mima", "");
        userName.setText(xm);
        userCode.setText(mm);
        
        if(xm.equals("")){
            chk.setChecked(false);
            System.out.println("88888,xm:"+xm);
        }else{
            System.out.println("999999,xm:"+xm);
            chk.setChecked(true);
        }
    }
    public void doClick(View v) {
        switch (v.getId()) {
        case R.id.btnLogin:
            String user = userName.getText().toString().trim();
            String code = userCode.getText().toString().trim();
            if (user.equals("wyl") && code.equals("123")) {
                if (chk.isChecked()) {

                    Editor edit = prefer.edit();
                    edit.putString("xingming", user);
                    edit.putString("mima", "123");
                    edit.commit();
                    System.out.println("===========");
                }else{
                    chk.setChecked(false);
                }
                System.out.println("dddddd");
                Toast.makeText(this, "登陆成功!", 100).show();
            }else{
                Editor e = prefer.edit();
                e.remove("xingming");
                e.remove("mima");
                e.commit();
                Toast.makeText(this, "登陆失败,请确认!", 100).show();
                String Xm = prefer.getString("xingming", "----");
                Toast.makeText(this, "姓名:"+Xm, 100).show();
            }
            break;
        default:
            break;
        }
    }

}

 

sharedPreferences

原文:http://www.cnblogs.com/Sunnor/p/4841611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!