一:运行效果图
初始页面:
勾选记住密码再次登录时
成功登陆时
密码不正确时
二:相关代码
(1)drawable下新建btn_select.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_shape" android:state_pressed="false"></item> <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item> </selector>
(2)drawable下新建btn_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF72CAE1"></solid> <corners android:radius="10dp"></corners> </shape>
(3)drawable下新建btn_shape_after.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#87CEFA"></solid> <corners android:radius="10dp"></corners> </selector>
(4)drawable下新建loginbg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45"/> </shape>
(5)drawable下新建logintop_roundbg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"></corners> <solid android:color="#55FFFFFF"></solid> </shape>
(6)values下新建strings.xml
<resources> <string name="app_name">Case_Login</string> <string name="etName">请输入账号</string> <string name="etPassword">请输入密码</string> <string name="btnLogin">登录</string> <string name="cbIsRememberPass">记住密码</string> <string name="btnRegister">注册</string> </resources>
(7)java下新建LoginActivity
public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbIsRememberPass; 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); cbIsRememberPass.setChecked(true); } } public void initViews(){ etName= (EditText) findViewById(R.id.etName); etPassword=(EditText)findViewById(R.id.etPassword) ; cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass); } 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(cbIsRememberPass.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();; } } }
tip:数据存储部分主要处理代码
(8)java下MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainactivity); } }
(1)layout下新建login_top.xml
<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:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:hint="@string/etName"> <requestFocus/> </EditText> <EditText android:id="@+id/etPassword" android:layout_below="@id/etName" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:hint="@string/etPassword"> <requestFocus/> </EditText> <LinearLayout android:layout_below="@id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cbIsRememberPass" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/cbIsRememberPass" android:textSize="20sp" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="login" android:text="@string/btnLogin" /> </LinearLayout> </RelativeLayout>(9)layout下新建activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/loginbg" tools:context="bzu.edu.cn.case_login.LoginActivity"> <include layout="@layout/login_top" android:id="@+id/include"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="131dp" android:layout_marginStart="131dp" android:layout_marginBottom="10dp" /> </RelativeLayout>
(10)layout下新建mainactivity_login.xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="40sp" android:text="Welcome you" /> </RelativeLayout>(11)编写配置文件AndroidManifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bzu.edu.cn.case_login"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"></activity> </application> </manifest>