首先实现界面的布局。
代码如下:
<?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:padding="@dimen/activity_horizontal_margin" android:background="@drawable/logintop_roundbg"> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawablePadding="10dp" android:background="@android:drawable/edit_text" android:ems="10" android:drawableLeft="@drawable/icon_user" android:hint="@string/etName" > <requestFocus/> </EditText> <EditText android:id="@+id/etPassword" android:inputType="textPassword" android:layout_below="@id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawablePadding="10dp" android:background="@android:drawable/edit_text" android:ems="10" android:drawableLeft="@drawable/icon_pass" android:hint="@string/etPass" > <requestFocus/> </EditText> <LinearLayout android:layout_below="@id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/checkBox" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="记住密码" /> <Button android:layout_width="0dp" android:onClick="login" android:layout_height="wrap_content" android:background="@drawable/btn_select" android:layout_weight="1" android:text="@string/btn1"/> </LinearLayout> </RelativeLayout>
设定初始密码及账号信息,在这里我们设置的 admin和123
我们实现登录之后将会出现欢迎登录界面。如图:(ps:已复制到剪切板是手机截图的原因,不是设计时出现的)
点击记住密码后登录,当退出再进入时还会有账号和密码的保存!
但是当输入的密码错误的时候时,将会出现吐司密码或账号错误的提示,即使点击记住密码当退出时账号和密码也不会保存。
实现的Java代码如下:
package com.example.renxiaohen.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 checkBox; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initViews(); sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false); if(isRemember){ String name=sharedPreferences.getString("name",""); String password= sharedPreferences.getString("password",""); etName.setText(name); etPassword.setText(password); checkBox.setChecked(true); } } private void initViews(){ etName =(EditText)findViewById(R.id.etName); etPassword =(EditText)findViewById(R.id.etPassword); checkBox=(CheckBox)findViewById(R.id.checkBox); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString(); if("admin".equals(name)&&"123".equals(password)){ SharedPreferences.Editor editor =sharedPreferences.edit(); if (checkBox.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).show(); } } }