zoukankan      html  css  js  c++  java
  • sharedPreferences

    效果图,

    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;
            }
        }
    
    }
  • 相关阅读:
    我非要捅穿这 Neutron(三)架构分析与代码实现篇(基于 OpenStack Rocky)
    我非要捅穿这 Neutron(二)上层资源模型篇
    $('.one + div')选取class为one的下一个元素
    15分钟,教你用Python爬网站数据,并用BI可视化分析!
    $("div span")选取里的所有的元素
    根据给定的元素名匹配元素
    根据给定的类名匹配元素
    根据给定的id匹配一个元素
    想创业,请问有没有投资小的项目?
    Vue组件间的通信
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4841611.html
Copyright © 2011-2022 走看看