1、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:background="@android:drawable/edit_text" android:ems="10" android:drawableLeft="@drawable/icon_user" android:hint="@string/etName" android:drawablePadding="10dp" /> <requestFocus /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etName" android:background="@android:drawable/edit_text" android:ems="10" android:drawableLeft="@drawable/icon_pass" android:hint="@string/etPass" android:inputType="textPassword" android:drawablePadding="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etPassword"> <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="记住密码" android:layout_marginLeft="10dp" android:id="@+id/check" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:background="@drawable/btn_select" android:text="@string/btnLogin" android:onClick="submit" android:id="@+id/btnsubmit"/> </LinearLayout> </RelativeLayout>
2、activity_login.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_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.case_login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/deer" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"/> </RelativeLayout>
3、LoginActivity交互代码
package cn.edu.bzu.case_login; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import java.util.HashMap; import java.util.Map; import cn.edu.bzu.case_login.model.ServiceShared; public class LoginActivity extends AppCompatActivity { private EditText edName; private EditText edPassword; private CheckBox check; private Button btnsubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); edName = (EditText) findViewById(R.id.etName); edPassword = (EditText) findViewById(R.id.etPassword); check = (CheckBox) findViewById(R.id.check); btnsubmit = (Button) findViewById(R.id.btnsubmit); ServiceShared serviceShared = new ServiceShared(this); HashMap data = serviceShared.getUserInfo(); if ((boolean) data.get("isremember")) { edName.setText(data.get("name").toString()); edPassword.setText(data.get("pass").toString()); check.setChecked(true); } } public void submit(View view) { if (edName.getText().toString().equals("")) {//判断账号是否为空 Toast.makeText(this, "账号不能为空!", Toast.LENGTH_SHORT).show(); return; } if (edPassword.getText().toString().equals("")) {//判断密码是否为空 Toast.makeText(this, "密码不能为空!", Toast.LENGTH_SHORT).show(); return; } if (check.isChecked()) {//选择记住密码并保存密码 ServiceShared ss = new ServiceShared(this); boolean temp = ss.save(edName.getText().toString(), edPassword.getText().toString()); if (temp) { Toast.makeText(this, "记住密码成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, SubmitActivity.class); intent.putExtra("name", edName.getText().toString()); startActivity(intent); } } else {//判断账号密码是否正确 ServiceShared serviceShared = new ServiceShared(this); HashMap data = serviceShared.getUserInfo(); String username = data.get("name").toString(); String password = data.get("pass").toString(); String name = edName.getText().toString(); String pass = edPassword.getText().toString(); if ((name.equals(username)) && (pass.equals(password))) { Intent intent = new Intent(this, SubmitActivity.class); intent.putExtra("name", edName.getText().toString()); startActivity(intent); } else { Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_LONG).show(); } } } }
4、activity_submit.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_submit" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.edu.bzu.case_login.SubmitActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_marginRight="10dp" android:background="@drawable/smile_blak" /> <TextView android:id="@+id/subtext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Welcom" android:textSize="36sp" /> </LinearLayout> </RelativeLayout>
5、SubmitActivity代码
package cn.edu.bzu.case_login; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class SubmitActivity extends AppCompatActivity { private TextView subtext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_submit); Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show(); subtext = (TextView) findViewById(R.id.subtext); Intent intent = getIntent(); subtext.setText("welcom " + intent.getStringExtra("name")); } }
6、ServiceShared.java工具类
package cn.edu.bzu.case_login.model; import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; /** * Created by Administrator on 2017/4/8. */ public class ServiceShared { private Context context; public ServiceShared(Context context) { this.context = context; } public boolean save(String name, String pass) {//存储信息 SharedPreferences sharedPreferences = context.getSharedPreferences("checkLogin.text", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("isremember", true); editor.putString("name", name); editor.putString("pass", pass); editor.commit(); return true; } public HashMap getUserInfo() {//读取信息 HashMap data = new HashMap(); SharedPreferences sharedPreferences = context.getSharedPreferences("checkLogin.text", Context.MODE_PRIVATE); String username = sharedPreferences.getString("name", null); String password = sharedPreferences.getString("pass", null); data.put("isremember", sharedPreferences.getBoolean("isremember", false)); data.put("name", username); data.put("pass", password); return data; } }
运行结果图