<?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_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:background="#8ECFE5"
>
<EditText
android:hint="请输入账号"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="54dp"
android:layout_marginStart="54dp"
android:layout_marginTop="33dp"
android:id="@+id/textView2"
/>
<EditText
android:text="请输入密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="10dp"
android:id="@+id/textView3" />
<CheckBox
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/checkBox"
android:layout_alignBottom="@+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/icon_user"
android:layout_above="@+id/textView3"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3"
android:id="@+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/icon_pass"
android:layout_alignBottom="@+id/textView3"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3"
android:id="@+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/deer"
android:id="@+id/imageView3"
android:layout_below="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="12dp" />
<Button
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:id="@+id/button"
android:layout_below="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="47dp"
android:layout_marginStart="47dp"
android:layout_alignRight="@+id/imageView3"
android:layout_alignEnd="@+id/imageView3"
android:background="@drawable/btn"
/>
</RelativeLayout>
package com.example.bz0209.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private Button button;
private CheckBox checkBox;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intView();
sharedPreferences=getSharedPreferences("login",MODE_PRIVATE);
boolean isRem=sharedPreferences.getBoolean("checkBox",false);
if(isRem){
String name=sharedPreferences.getString("name","");
String pass=sharedPreferences.getString("pass","");
editText1.setText(name);
editText2.setText(pass);
checkBox.isChecked(true);
}
}
private void intView(){
editText1=(EditText) findViewById(R.id.textView2);
editText2=(EditText) findViewById(R.id.textView3);
button=(Button) findViewById(R.id.button);
checkBox=(CheckBox) findViewById(R.id.checkBox);
}
private void login(){
String name=editText1.getText().toString();
String pass=editText2.getText().toString();
SharedPreferences.Editor editor=sharedPreferences.edit();
if(checkBox.isChecked()){
editor.putBoolean("checkBox",true);
editor.putString("name",name);
editor.putString("pass",pass);
}
else
editor.clear();
editor.commit();
if("admin".equals(name)&&"pass".equals(pass)){
Intent intent=new Intent(this,welcome.class);
startActivity(intent);
finish();
}
else
Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG).show();
}
package com.example.bz0209.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class welcome extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
<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_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:background="#8ECFE5"
>
<EditText
android:hint="请输入账号"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="54dp"
android:layout_marginStart="54dp"
android:layout_marginTop="33dp"
android:id="@+id/textView2"
/>
<EditText
android:text="请输入密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="10dp"
android:id="@+id/textView3" />
<CheckBox
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/checkBox"
android:layout_alignBottom="@+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/icon_user"
android:layout_above="@+id/textView3"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3"
android:id="@+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/icon_pass"
android:layout_alignBottom="@+id/textView3"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3"
android:id="@+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/deer"
android:id="@+id/imageView3"
android:layout_below="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="12dp" />
<Button
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:id="@+id/button"
android:layout_below="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="47dp"
android:layout_marginStart="47dp"
android:layout_alignRight="@+id/imageView3"
android:layout_alignEnd="@+id/imageView3"
android:background="@drawable/btn"
/>
</RelativeLayout>
package com.example.bz0209.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private Button button;
private CheckBox checkBox;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intView();
sharedPreferences=getSharedPreferences("login",MODE_PRIVATE);
boolean isRem=sharedPreferences.getBoolean("checkBox",false);
if(isRem){
String name=sharedPreferences.getString("name","");
String pass=sharedPreferences.getString("pass","");
editText1.setText(name);
editText2.setText(pass);
checkBox.isChecked(true);
}
}
private void intView(){
editText1=(EditText) findViewById(R.id.textView2);
editText2=(EditText) findViewById(R.id.textView3);
button=(Button) findViewById(R.id.button);
checkBox=(CheckBox) findViewById(R.id.checkBox);
}
private void login(){
String name=editText1.getText().toString();
String pass=editText2.getText().toString();
SharedPreferences.Editor editor=sharedPreferences.edit();
if(checkBox.isChecked()){
editor.putBoolean("checkBox",true);
editor.putString("name",name);
editor.putString("pass",pass);
}
else
editor.clear();
editor.commit();
if("admin".equals(name)&&"pass".equals(pass)){
Intent intent=new Intent(this,welcome.class);
startActivity(intent);
finish();
}
else
Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG).show();
}
package com.example.bz0209.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class welcome extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
}