zoukankan      html  css  js  c++  java
  • Android_sharePreference_ex1

    xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           >
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="用户名:"
               android:textSize="20sp"
           />
           <EditText 
               android:id="@+id/user"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:textSize="20sp"
           />
       </LinearLayout>
        <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           >
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="密   码:"
               android:textSize="20sp"
           />
           <EditText 
               android:id="@+id/pass"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:textSize="20sp"
           />
       </LinearLayout>
        <CheckBox 
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:checked="false"
            android:text="保存密码"
            android:textSize="20sp"
         />
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
          <Button 
              android:id="@+id/login"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="登录"
            android:onClick="Click"
              android:textSize="20sp"/>
            <Button 
              android:id="@+id/cancel"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="取消"
            android:onClick="Click"
              android:textSize="20sp"/>
        </LinearLayout>
    </LinearLayout>

    源代码:

    package com.example.sharepreferencesdemo;
    
    import java.util.prefs.Preferences;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;
    /**
     * 利用preferences保存登录的用户名
     * @author Administrator
     *
     */
    public class sharePreferences_example extends Activity{
            private EditText user;
            private EditText pass;
            private Button login;
            private Button cancel;
            private CheckBox check;
            private Editor editor;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.pref_examle);
                user = (EditText) findViewById(R.id.user);
                pass = (EditText) findViewById(R.id.pass);
                login = (Button) findViewById(R.id.login);
                cancel = (Button) findViewById(R.id.cancel);
                check = (CheckBox) findViewById(R.id.check);
                SharedPreferences pref =  getSharedPreferences("userInfo",MODE_PRIVATE);
                editor = pref.edit();
                String name = pref.getString("userName", "");
                if(name.equals("")){
                    check.setChecked(false);
                }else{
                    check.setChecked(true);
                    user.setText(name);
                }
                
            }
            public void Click(View view){
                String userName = user.getText().toString().trim();
                String passWord = pass.getText().toString();
                switch(view.getId()){
                case R.id.login:
                    if("zhangsan".equals(userName)&&"123456".equals(passWord)){
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        if(check.isChecked()){
                            editor.putString("userName", userName);
                            editor.commit();
                        }else{
                            editor.remove("userName");
                            editor.commit();
                        }
                    }else{
                        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.cancel:
                    break;
                }
            }
    }
  • 相关阅读:
    如何使用观测者模式实现监控和推送
    oracle在desc表时,name 和type列不能格式化问题(占位过长)
    [置顶] Vim用正则表达式进行批量修改
    Eclipse扩展点
    写给C语言新手的话
    QQ圈子降级为“应用”后应关注其隐私设置
    win8vs2012创建自带sqlServer数据库出错
    JQuery 选择器
    SINGLETON(单例模式)---(孤独的人)
    C++中的常对象和常对象成员
  • 原文地址:https://www.cnblogs.com/fangg/p/5583715.html
Copyright © 2011-2022 走看看