本次文章主要掌握SharedPreferences的使用。
为了完成这个项目我们需要建立两个布局
activity_login:
<?xml version="1.0" encoding="utf-8"?>
<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: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="cn.edu.bzu.case_login.Login_Activity">
<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: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="cn.edu.bzu.case_login.Login_Activity">
<include
android:id="@+id/include1"
layout="@layout/login_top"></include>
android:id="@+id/include1"
layout="@layout/login_top"></include>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
app:srcCompat="@drawable/deer" />
</RelativeLayout>
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
app:srcCompat="@drawable/deer" />
</RelativeLayout>
login_top:
<?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/logintop_roundbg"
android:padding="@dimen/activity_horizontal_margin">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logintop_roundbg"
android:padding="@dimen/activity_horizontal_margin">
<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:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etName" />
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:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etName" />
<requestFocus />
<EditText
android:id="@+id/etPass"
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:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etPassword"
android:inputType="textPassword" />
android:id="@+id/etPass"
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:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etPassword"
android:inputType="textPassword" />
<requestFocus />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPass">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPass">
<CheckBox
android:text="记住密码"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/rememberpass"
android:layout_weight="1"
android:textSize="20sp"/>
android:text="记住密码"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/rememberpass"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="@string/btnLogin"
android:onClick="login"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="@string/btnLogin"
android:onClick="login"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome you"
android:textSize="50sp"
android:layout_gravity="center"
android:layout_marginTop="100dp"/>
</LinearLayout>