Login.java
1 package com.example.tiwenzhuce; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.text.Editable; 8 import android.text.TextWatcher; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 import java.util.Map; 15 16 17 public class Login extends AppCompatActivity { 18 private Button mBtnLogin; 19 private EditText mEtUserName;//声明空间 20 private EditText et_1; 21 private EditText et_2; 22 private String strname; 23 private String strpasswd; 24 private SharedHelper sh; 25 private Context mContext; 26 private Button mBtnzhuce1; 27 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 mContext = getApplicationContext(); 32 sh = new SharedHelper(mContext); 33 et_1=findViewById(R.id.et_1); 34 et_2=findViewById(R.id.et_2); 35 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_login); 38 mBtnLogin = findViewById(R.id.btn_login); 39 mBtnzhuce1=findViewById(R.id.btn_zh_1);//查找 40 mBtnLogin.setOnClickListener(new View.OnClickListener() 41 { 42 @Override 43 public void onClick(View v){ 44 strname=et_1.getText().toString(); 45 strpasswd=et_2.getText().toString(); 46 sh.save(strname,strpasswd); 47 Toast.makeText(Login.this,"登录成功!",Toast.LENGTH_SHORT).show(); 48 Intent intent = new Intent(Login.this,TiWenzhuce.class); 49 startActivity(intent); 50 } 51 }); 52 mBtnzhuce1.setOnClickListener(new View.OnClickListener() 53 { 54 @Override 55 public void onClick(View v){ 56 Intent intent =new Intent(Login.this,zhuceActivity.class); 57 startActivity(intent); 58 } 59 }); 60 61 mEtUserName = findViewById(R.id.et_1);//监听事件 62 mEtUserName.addTextChangedListener(new TextWatcher() { 63 @Override 64 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 65 66 } 67 68 @Override 69 public void onTextChanged(CharSequence s, int start, int before, int count) { 70 Log.d("edittext",s.toString()); 71 } 72 73 @Override 74 public void afterTextChanged(Editable s) { 75 76 } 77 }); 78 } 79 @Override 80 protected void onStart() { 81 super.onStart(); 82 Map<String,String> data = sh.read(); 83 et_1.setText(data.get("username")); 84 et_2.setText(data.get("passwd")); 85 } 86 }
activity_login
<?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="match_parent" android:padding="15dp"> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:textSize="16sp" android:hint="用户名:" android:drawableLeft="@drawable/yonghum" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:maxLines="1" android:drawablePadding="7dp" android:layout_marginTop="25dp" android:layout_height="50dp"/> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:textSize="16sp" android:hint="密码:" android:inputType="textPassword" android:layout_below="@id/et_1" android:layout_height="50dp" android:drawableLeft="@drawable/mimasuo" android:maxLines="1" android:layout_marginTop="25dp" android:drawablePadding="7dp" android:background="@drawable/bg_username" android:paddingLeft="10dp"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/et_2" android:layout_marginTop="25dp" android:text="登录" android:textSize="25sp" android:textColor="#fff" android:background="@drawable/bg_btn4"/> <Button android:id="@+id/btn_zh_1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/btn_login" android:layout_marginTop="25dp" android:text="注册" android:textSize="25sp" android:textColor="#fff" android:background="@drawable/bg_btn4"/> </RelativeLayout>