activity_top 顶端布局
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="com.example.bz0209.myapplication.MainActivity">
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="please input number" />
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="please input password"
android:inputType="textPassword" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etPass">
android:id="@+id/cbIsRememberPass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记住密码"
android:textSize="14sp" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_weight="1"
android:onClick="login"
android:text="登录" />
activity_login 登录布局
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="#282"
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="com.example.bz0209.myapplication.MainActivity">
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"
app:srcCompat="@drawable/deer" />
activity_main 欢迎界面
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="com.example.bz0209.myapplication.MainActivity">
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="192dp"
android:ems="10"
android:inputType="textPersonName"
android:text=" Welcome to here"
android:textSize="30sp" />
MainActivity 控制代码
package com.example.bz0209.myapplication;
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 MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etPass;
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 pass=sharedPreferences.getString("pass","");
etName.setText(name);
etPass.setText(pass);
cbIsRememberPass.setChecked(true);
}
}
private void initViews() {
etName=(EditText) findViewById(R.id.etName);
etPass=(EditText)findViewById(R.id.etPass);
cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass);
}
public void login(View view){
String name=etName.getText().toString();
String pass=etPass.getText().toString();
if("admin".equals(name)&&"123456".equals(pass)){
SharedPreferences.Editor editor=sharedPreferences.edit();
if(cbIsRememberPass.isChecked()){
editor.putBoolean("rememberPassword",true);
editor.putString("name",name);
editor.putString("pass",pass);
}else{
editor.clear();
}
editor.commit();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();
}
}
}