在layout下新建一个名为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: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/etPass" >
<requestFocus/>
</EditText>
<LinearLayout android:layout_below="@id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content">
<CheckBox android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/cbIsRememberPass" android:textSize="20sp" android:layout_weight="1"/>
/>
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="login" android:text="登录"/>
</LinearLayout>
</RelativeLayout>
在layout下新建一个名为activity_login.xml的文件
<?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: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="cn.edu.bzu.a01.LoginActivity">
<include layout="@layout/login_top"></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_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="12dp" android:layout_marginEnd="12dp" android:layout_marginBottom="32dp" />
</RelativeLayout>
在layout下新建一个名为activity_main.xml的文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" tools:context="cn.edu.bzu.a01.MainActivity">
<TextView android:text="Welcome you" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textSize="40sp" android:id="@+id/textView" />
</RelativeLayout>
在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>
在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>
在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>
在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>
在values下的strings.xml文件中
<resources>
<string name="app_name">01</string>
<string name="etName">请输入账号</string>
<string name="etPass">请输入密码</string>
<string name="btnLogin">登录</string>
<string name="btnRegister">注册</string>
</resources>
在java下的LoginActivity文件下
package cn.edu.bzu.a01;
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 cbIsRememberPass;
private SharedPreferences sharedPreferences;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
boolean isRemember=sharedPreferences.getBoolean("remamberpassword",false);
if (isRemember){
String name=sharedPreferences.getString("name","");
String password=sharedPreferences.getString("password","");
etName.setText(name);
etPassword.setText(password);
cbIsRememberPass.setChecked(true);
}
}
private void initView() {
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("remamberpassword",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_SHORT).show();
}
}
}
在java下新建一个Activity-MainActivity文件
package cn.edu.bzu.a01;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}