第八次作业

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#55000000"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp" >

<LinearLayout
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QQ"
android:textColor="#fff"
android:textSize="50dp" />
</LinearLayout>
<EditText
android:id="@+id/userid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@null"
android:hint="QQ号/手机号/邮箱"
android:maxLength="13"
android:singleLine="true"
android:textColor="#fff"
android:textSize="25sp"
android:textColorHint="#eee" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:background="#eee" />
<EditText
android:id="@+id/lipassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@null"
android:hint="密码"
android:inputType="textPassword"
android:maxLength="13"
android:singleLine="true"
android:textColor="#fff"
android:textSize="25dp"
android:textColorHint="#eee" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:background="#eee" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#aaafff"
android:text="登录"
android:onClick="onClick"
android:textColor="#fff"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/cbRemember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="记住密码"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="新用户注册"
android:textColor="#cc1CA4DE"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>

</android.support.constraint.ConstraintLayout>
java
package com.example.denglu;

import android.app.Notification;
import android.content.SharedPreferences;
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;

public class MainActivity extends AppCompatActivity {

EditText userid; //声明控件
EditText lipassword;
Button login;
private CheckBox cbRemember;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userid=(EditText)findViewById(R.id.userid); //控件的初始化
lipassword=(EditText)findViewById(R.id.lipassword);
login=(Button)findViewById(R.id.login); //监听
cbRemember = findViewById(R.id.cbRemember);
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
userid.setText(sp.getString("name", ""));
lipassword.setText(sp.getString("password", ""));



login.setOnClickListener(new View.OnClickListener() {
@Override //方法的调用
public void onClick(View view) {

if ("admin".equals(userid.getText().toString())&&"admin".equals(lipassword.getText().toString())) {
if (cbRemember.isChecked()) {
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", userid.getText().toString());
editor.putString("password", lipassword.getText().toString());
editor.commit();
Toast t1 = Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT);
t1.show();
} else {
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", "");
editor.putString("password", "");
editor.commit();
Toast t3 = Toast.makeText(getApplicationContext(), "登录成功,保存失败", Toast.LENGTH_SHORT);
t3.show();

}
}
else {
Toast t2 = Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT);
t2.show();
userid.setText("");
lipassword.setText("");
}
}
});
}

}

图片

 

原文地址:https://www.cnblogs.com/lixiaoai/p/11770809.html