2024.3.27
第十七天
所花时间:2小时
代码量:300+
博客量:1
了解到的知识点:个人学习记录app——登陆注册1
package com.hui.study; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.hui.study.dao.StudentDao; import com.hui.study.entity.Student; /** * function:连接页面加载首页 */ public class MainActivity extends AppCompatActivity { private static final String TAG = "mysql-party-MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void reg(View view){ startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); } /** * function: 登录 * */ public void login(View view){ EditText EditTextAccount = findViewById(R.id.studentid); EditText EditTextPhone = findViewById(R.id.phone); EditText EditTextClassroom = findViewById(R.id.classroom); new Thread(){ @Override public void run() { StudentDao studentDao = new StudentDao(); int msg = studentDao.login(EditTextAccount.getText().toString(),EditTextPhone.getText().toString(),EditTextClassroom.getText().toString()); hand1.sendEmptyMessage(msg); if(msg == 1){ } } }.start(); } @SuppressLint("HandlerLeak") final Handler hand1 = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0){ Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show(); } else if (msg.what == 1) { Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show(); // 实验导入用户名 // 在用户登录成功后保存用户名到 SharedPreferences 中 // SharedPreferences sharedPreferences = getSharedPreferences("userData", MODE_PRIVATE); // SharedPreferences.Editor editor = sharedPreferences.edit(); // editor.putString("username", ); // editor.apply(); //试验一下!!!!!!!!!!!!!!!!!!!!!! startActivity(new Intent(getApplicationContext(),RecordActivity.class)); } else if (msg.what == 2){ Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show(); } else if (msg.what == 3){ Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show(); } } }; }
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:orientation="vertical" 14 tools:layout_editor_absoluteX="219dp" 15 tools:layout_editor_absoluteY="207dp" 16 android:padding="50dp" 17 18 > 19 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:orientation="horizontal"> 24 25 26 <TextView 27 android:id="@+id/textView" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_weight="1" 31 android:textSize="15sp" 32 android:text="用户ID(学号):" /> 33 34 <EditText 35 android:id="@+id/studentid" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:ems="10" 40 android:inputType="phone" 41 android:text="" /> 42 </LinearLayout> 43 <LinearLayout 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:orientation="horizontal"> 47 48 49 <TextView 50 android:id="@+id/textView2" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:layout_weight="1" 54 android:textSize="15sp" 55 android:text="电话:" /> 56 57 <EditText 58 android:id="@+id/phone" 59 android:layout_width="wrap_content" 60 android:layout_height="wrap_content" 61 android:layout_weight="1" 62 android:ems="10" 63 android:inputType="phone" 64 android:text="" /> 65 </LinearLayout> 66 <LinearLayout 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content" 69 android:orientation="horizontal"> 70 71 72 <TextView 73 android:id="@+id/textView3" 74 android:layout_width="wrap_content" 75 android:layout_height="wrap_content" 76 android:layout_weight="1" 77 android:textSize="15sp" 78 android:text="班级:" 79 80 /> 81 82 <EditText 83 android:id="@+id/classroom" 84 android:layout_width="wrap_content" 85 android:layout_height="wrap_content" 86 android:layout_weight="1" 87 android:ems="10" 88 android:inputType="textPersonName" 89 /> 90 </LinearLayout> 91 <LinearLayout 92 android:layout_width="match_parent" 93 android:layout_height="wrap_content" 94 android:orientation="horizontal"> 95 96 97 98 </LinearLayout> 99 100 <Button 101 android:layout_marginTop="50dp" 102 android:id="@+id/button2" 103 android:layout_width="match_parent" 104 android:layout_height="wrap_content" 105 android:text="登录" 106 android:onClick="login" 107 /> 108 109 <Button 110 android:id="@+id/button3" 111 android:layout_width="match_parent" 112 android:layout_height="wrap_content" 113 android:onClick="reg" 114 android:text="注册" /> 115 </LinearLayout> 116 </androidx.constraintlayout.widget.ConstraintLayout>