Case Login 记住密码 完整版

来源:互联网 发布:上古卷轴5御姐捏脸数据 编辑:程序博客网 时间:2024/09/21 06:38

package cn.edu.bzu.case_login;//主代码import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity {    private EditText etName;    private EditText etPassword;    private CheckBox cbIsRememberRass;    private SharedPreferences sharedPreferences;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        initView();        sharedPreferences = getSharedPreferences("renmemberpassword", Context.MODE_APPEND);        boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);        if (isRemember){            String name=sharedPreferences.getString("name","");            String password=sharedPreferences.getString("password","");            etName.setText( name);            etPassword.setText(password);            cbIsRememberRass.setChecked(true);        }    }    private void initView() {        etName=(EditText)findViewById(R.id.etName);        etPassword=(EditText)findViewById(R.id.etPassword);        cbIsRememberRass=(CheckBox) findViewById(R.id.cbIsRememberRass);    }    public void login(View view){        String name=etName.getText().toString();        String password=etPassword.getText().toString();        if ("admin".equals(name)&&"123456".equals(password)){            SharedPreferences.Editor editor=sharedPreferences.edit();            if (cbIsRememberRass.isChecked()){                editor.putBoolean("rememberpassword",true);                editor.putString("name",name);                editor.putString("password",password);            }else {                editor.clear();            }            editor.commit();            Intent intent=new Intent(this,MainActivity.class);            startActivity(intent);            finish();        }else {            Toast.makeText(this,"账号密码有错误",Toast.LENGTH_LONG);        }    }}
?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="match_parent">    <EditText        android:id="@+id/etName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@android:drawable/edit_text"        android:drawableLeft="@drawable/icon_user"        android:ems="10"        android:hint="@string/etName">        <requestFocus />    </EditText>    <EditText        android:id="@+id/etPassword"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/etName"        android:background="@android:drawable/edit_text"        android:drawableLeft="@drawable/icon_pass"        android:ems="10"        android:hint="@string/etPass"        android:inputType="textPassword">        <requestFocus />    </EditText>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/etPassword"        android:id="@+id/linearLayout">        <CheckBox            android:text="记住密码"            android:textSize="20sp"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/cbIsRememberRass" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@drawable/btn_select"            android:text="@string/btnLogin" />    </LinearLayout></RelativeLayout>
//主页面
0 0
原创粉丝点击