第八次作业

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/abc" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="30sp" />

<EditText
android:id="@+id/etid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="30sp" />

<EditText
android:id="@+id/etpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp">

<CheckBox
android:id="@+id/Remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="记住密码"
android:textSize="20sp" />

<Button
android:id="@+id/Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="Login"
android:text="登录"
android:textSize="20sp" />

<Button
android:id="@+id/Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="取消"
android:textSize="20sp" />

</LinearLayout>


</LinearLayout>


package com.example.myapplication;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText etid;
private EditText etpwd;
private CheckBox Remember;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etid = findViewById(R.id.etid);
etpwd = findViewById(R.id.etpwd);
Remember = findViewById(R.id.Remember);
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
etid.setText(sp.getString("name", ""));
etpwd.setText(sp.getString("password", ""));

}

public void Login(View view) {
if ("962397002".equals(etid.getText().toString()) && "isme123".equals(etpwd.getText().toString())) {
if (Remember.isChecked()) {
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", etid.getText().toString());
editor.putString("password", etpwd.getText().toString());
editor.commit();
Toast.makeText(this, "登陆成功,已保存", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", "");
editor.putString("password", "");
editor.commit();
Toast.makeText(this, "登陆成功,未保存", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "密码错误,请重试", Toast.LENGTH_SHORT).show();
etid.setText("");
etpwd.setText("");
}
}
}


 

 

 



posted @ 2019-11-13 22:17  IS'ME  阅读(82)  评论(0)    收藏  举报