记住密码

实验结果


LoginActivity.java

public class LoginActivity extends Activity {  
      
    private SharedPreferences pref;  
    private SharedPreferences.Editor editor;  
    private CheckBox rememberPass;  
    private EditText accountEdit;  
    private EditText passwordEdit;  
    private Button btnLogin;  
    private TextWatcher watcher;  
    private TextWatcher textWatcher;  
      
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_login);  
        pref = PreferenceManager.getDefaultSharedPreferences(this);  
        accountEdit = (EditText) findViewById(R.id.etUsername);  
          
        passwordEdit = (EditText)findViewById( R.id.etPassword);  
        rememberPass = (CheckBox)findViewById(R.id.cbRememberPass);  
        btnLogin = (Button)findViewById(R.id.btnLogin);  
        accountEdit.addTextChangedListener(new TextWatcher() {  
              
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
                // TODO Auto-generated method stub  
                String edaccount = s.toString();  
                String isaccount = pref.getString("account","");  
                if(isaccount.equals(edaccount)){  
                    String password = pref.getString("password", "" );  
                    passwordEdit.setText(password);  
                    rememberPass.setChecked(true);  
                }  
            }  
              
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,  
                    int after) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            @Override  
            public void afterTextChanged(Editable s) {  
                // TODO Auto-generated method stub  
              
            }  
        });  
        btnLogin.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                String account = accountEdit.getText().toString();  
                String password = passwordEdit.getText().toString();  
                if(account.equals("admin")&&password.equals("123456")){  
                    editor = pref.edit();  
                    if(rememberPass.isChecked()){  
                        editor.putBoolean("remember_password", true);  
                        editor.putString("account", account);  
                        editor.putString("password", password);  
                        editor.commit();
                          
                    }else{  
                        editor.clear();  
                     }  
                      
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);  
                    startActivity(intent);  
                    //finish();  
                }else{  
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();  
                }  
                  
            }  
        });  
          
    }  
  
}  


activity_login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:background="@drawable/loginbg"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".LoginActivity" >  
  
  <include layout="@layout/login_top"/>   
  <include layout="@layout/login_bottom"/>
  
</LinearLayout>  


login_top.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:background="@drawable/btnbg_roundcorner"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin" >  
  
    <TextView  
        android:id="@+id/tvUsername"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:text="用户名"  
        android:textAppearance="?android:attr/textAppearanceMedium" />  
  
    <EditText  
        android:id="@+id/etUsername"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/tvUsername"  
        android:layout_below="@+id/tvUsername"  
        android:background="@android:drawable/edit_text"  
        android:ems="10" >  
  
        <requestFocus />  
    </EditText>  
  
    <TextView  
        android:id="@+id/tvPassword"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/etUsername"  
        android:layout_below="@+id/etUsername"  
        android:text="密码"  
        android:textAppearance="?android:attr/textAppearanceMedium" />  
  
    <EditText  
        android:id="@+id/etPassword"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/tvPassword"  
        android:layout_below="@+id/tvPassword"  
        android:background="@android:drawable/edit_text"  
        android:ems="10"  
        android:inputType="textPassword" />  
  
    <CheckBox  
        android:id="@+id/cbRememberPass"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/btnLogin"  
        android:layout_alignBottom="@+id/btnLogin"  
        android:layout_alignLeft="@+id/etPassword"  
        android:text="记住密码"  
        android:textSize="20sp" />  
  
    <Button  
        android:id="@+id/btnLogin"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignRight="@+id/etPassword"  
        android:layout_below="@+id/etPassword"  
        android:background="#FF72CAE1"  
        android:onClick="btnLogin"  
        android:text="登陆" />  
  
</RelativeLayout>  


login_bottom.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content" >  
  
    <TextView  
        android:id="@+id/tvRegist"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="21dp"  
        android:layout_marginTop="18dp"  
        android:text="  "  
        android:autoLink="all"  
        android:textColorLink="#FF0066CC" />  
  
    <ImageView  
        android:id="@+id/imageView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentRight="true"  
        android:layout_marginBottom="24dp"  
        android:src="@drawable/panda" />  
  
    <ImageView  
        android:id="@+id/imageView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginBottom="28dp"  
        android:src="@drawable/icon" />  
  
</RelativeLayout>  

SharedPreferences是Android平台上一个存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。SharedPreferences是一个接口,在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值